diff --git a/Dalamud/Utility/Hash.cs b/Dalamud/Utility/Hash.cs new file mode 100644 index 000000000..92f03da7e --- /dev/null +++ b/Dalamud/Utility/Hash.cs @@ -0,0 +1,34 @@ +using System; + +namespace Dalamud.Utility +{ + /// + /// Utility functions for hashing. + /// + public static class Hash + { + /// + /// Get the SHA-256 hash of a string. + /// + /// The string to hash. + /// The computed hash. + internal static string GetStringSha256Hash(string text) + { + return string.IsNullOrEmpty(text) ? string.Empty : GetSha256Hash(System.Text.Encoding.UTF8.GetBytes(text)); + } + + /// + /// Get the SHA-256 hash of a byte array. + /// + /// The byte array to hash. + /// The computed hash. + internal static string GetSha256Hash(byte[] buffer) + { + using var sha = new System.Security.Cryptography.SHA256Managed(); + var hash = sha.ComputeHash(buffer); + return ByteArrayToString(hash); + } + + private static string ByteArrayToString(byte[] ba) => BitConverter.ToString(ba).Replace("-", string.Empty); + } +}