mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
feat: hashing utils
This commit is contained in:
parent
597b7bb000
commit
5d90973386
1 changed files with 34 additions and 0 deletions
34
Dalamud/Utility/Hash.cs
Normal file
34
Dalamud/Utility/Hash.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Dalamud.Utility
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Utility functions for hashing.
|
||||||
|
/// </summary>
|
||||||
|
public static class Hash
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Get the SHA-256 hash of a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to hash.</param>
|
||||||
|
/// <returns>The computed hash.</returns>
|
||||||
|
internal static string GetStringSha256Hash(string text)
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(text) ? string.Empty : GetSha256Hash(System.Text.Encoding.UTF8.GetBytes(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the SHA-256 hash of a byte array.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="buffer">The byte array to hash.</param>
|
||||||
|
/// <returns>The computed hash.</returns>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue