patbef-ServiceInside/Support/Crypto/SHA512.cs

73 lines
1.8 KiB
C#
Raw Normal View History

2024-01-29 16:26:54 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Support.Crypto
{
public static class SHA512
{
public static string Encrypt(string value)
{
string result = null;
try
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
using (System.Security.Cryptography.SHA512 hash = System.Security.Cryptography.SHA512.Create())
{
byte[] hashedInputBytes = hash.ComputeHash(bytes);
StringBuilder hashedInputStringBuilder = new StringBuilder(128);
foreach (var b in hashedInputBytes)
{
hashedInputStringBuilder.Append(b.ToString("X2"));
}
result = hashedInputStringBuilder.ToString().ToLower();
}
}
catch
{
result = null;
}
return result;
}
public static string HMAC(string message, byte[] key)
{
string result = null;
try
{
StringBuilder hash = new StringBuilder();
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (HMACSHA512 hmac = new HMACSHA512(key))
{
byte[] hashValue = hmac.ComputeHash(messageBytes);
foreach (var theByte in hashValue)
{
hash.Append(theByte.ToString("x2"));
}
}
return hash.ToString();
}
catch
{
result = null;
}
return result;
}
}
}