2017-04-15 08:28:34 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Teknik.Utilities.Cryptography
|
|
|
|
|
{
|
|
|
|
|
public class SHA256
|
|
|
|
|
{
|
|
|
|
|
public static string Hash(string value)
|
|
|
|
|
{
|
2017-06-09 19:41:00 +02:00
|
|
|
|
byte[] valueBytes = Encoding.UTF8.GetBytes(value);
|
2017-04-15 08:28:34 +02:00
|
|
|
|
return Hash(valueBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Hash(byte[] value)
|
|
|
|
|
{
|
2022-05-23 05:51:09 +02:00
|
|
|
|
var hash = System.Security.Cryptography.SHA256.Create();
|
2017-04-15 08:28:34 +02:00
|
|
|
|
byte[] hashBytes = hash.ComputeHash(value);
|
|
|
|
|
|
|
|
|
|
return Convert.ToBase64String(hashBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] Hash(Stream value)
|
|
|
|
|
{
|
2022-05-23 05:51:09 +02:00
|
|
|
|
var hash = System.Security.Cryptography.SHA256.Create();
|
2017-04-15 08:28:34 +02:00
|
|
|
|
return hash.ComputeHash(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Hash(string value, string salt1, string salt2)
|
|
|
|
|
{
|
2022-05-23 05:51:09 +02:00
|
|
|
|
var hash = System.Security.Cryptography.SHA256.Create();
|
2017-04-15 08:28:34 +02:00
|
|
|
|
// gen salt2 hash
|
|
|
|
|
byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
|
|
|
|
|
byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
|
|
|
|
|
string salt2Str = string.Empty;
|
|
|
|
|
foreach (byte x in salt2Bytes)
|
|
|
|
|
{
|
2022-05-23 05:51:09 +02:00
|
|
|
|
salt2Str += string.Format("{0:x2}", x);
|
2017-04-15 08:28:34 +02:00
|
|
|
|
}
|
|
|
|
|
string dataStr = salt1 + value + salt2Str;
|
2020-07-17 09:48:24 +02:00
|
|
|
|
string sha1Str = SHA1.Hash(dataStr);
|
2017-04-15 08:28:34 +02:00
|
|
|
|
byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
|
|
|
|
|
byte[] valueBytes = hash.ComputeHash(sha1Bytes);
|
|
|
|
|
string hashString = string.Empty;
|
|
|
|
|
foreach (byte x in valueBytes)
|
|
|
|
|
{
|
2022-05-23 05:51:09 +02:00
|
|
|
|
hashString += string.Format("{0:x2}", x);
|
2017-04-15 08:28:34 +02:00
|
|
|
|
}
|
|
|
|
|
return hashString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|