Ooui-tws-port/Ooui/Utilities.cs

61 lines
1.5 KiB
C#
Raw Normal View History

2018-03-10 05:28:39 +00:00
using System;
2019-02-04 22:22:45 +00:00
using System.Security.Cryptography;
2018-03-10 05:28:39 +00:00
using System.Text;
namespace Ooui
{
public static class Utilities
{
[ThreadStatic]
2019-02-04 22:22:45 +00:00
static SHA256 sha256;
2018-03-10 05:28:39 +00:00
2019-02-04 22:22:45 +00:00
[ThreadStatic]
static MD5 md5;
public static string GetShaHash(byte[] bytes)
2018-03-10 05:28:39 +00:00
{
var sha = sha256;
2019-02-04 22:22:45 +00:00
if (sha == null)
{
sha = SHA256.Create();
2018-03-10 05:28:39 +00:00
sha256 = sha;
}
2019-02-04 22:22:45 +00:00
var data = sha.ComputeHash(bytes);
return BytesToString(data);
}
public static string GetMd5Hash(string input)
{
var md = md5;
if (md == null)
{
md = MD5.Create();
md5 = md;
2018-03-10 05:28:39 +00:00
}
2019-02-04 22:22:45 +00:00
// Convert the input string to a byte array and compute the hash.
byte[] data = md.ComputeHash(Encoding.UTF8.GetBytes(input));
return BytesToString(data);
}
private static string BytesToString(byte[] data)
{
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
2018-03-10 05:28:39 +00:00
}
}
}