mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[DatTools, RomTools] Create method for cleaning file hashes
This commit is contained in:
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SabreTools.Helper
|
||||
{
|
||||
@@ -317,5 +318,28 @@ namespace SabreTools.Helper
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clean a hash string and pad to the correct size
|
||||
/// </summary>
|
||||
/// <param name="hash">Hash string to sanitize</param>
|
||||
/// <param name="padding">Amount of characters to pad to</param>
|
||||
/// <returns>Cleaned string</returns>
|
||||
public static string CleanHashData(string hash, int padding)
|
||||
{
|
||||
// First get the hash to the correct length
|
||||
hash = (String.IsNullOrEmpty(hash) ? "" : hash.Trim());
|
||||
hash = (hash.StartsWith("0x") ? hash.Remove(0, 2) : hash);
|
||||
hash = (hash == "-" ? "" : hash);
|
||||
hash = (String.IsNullOrEmpty(hash) ? "" : hash.PadLeft(padding, '0'));
|
||||
|
||||
// Then make sure that it has the correct characters
|
||||
if (!Regex.IsMatch(hash.ToLowerInvariant(), "[0-9a-f]{" + padding + "}"))
|
||||
{
|
||||
hash = "";
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user