2014-04-28 06:34:29 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
2014-04-30 23:39:54 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2019-03-29 02:20:40 +00:00
|
|
|
|
public static class Hashing
|
2014-04-30 23:39:54 +00:00
|
|
|
|
{
|
|
|
|
|
public static string SHA256Hash(this string input)
|
|
|
|
|
{
|
2021-07-30 04:47:53 +00:00
|
|
|
|
using (var hash = SHA256.Create())
|
2014-04-30 23:39:54 +00:00
|
|
|
|
{
|
|
|
|
|
var enc = Encoding.UTF8;
|
2014-04-28 06:34:29 +00:00
|
|
|
|
return GetHash(hash.ComputeHash(enc.GetBytes(input)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string SHA256Hash(this Stream input)
|
|
|
|
|
{
|
2021-07-30 04:47:53 +00:00
|
|
|
|
using (var hash = SHA256.Create())
|
2014-04-28 06:34:29 +00:00
|
|
|
|
{
|
|
|
|
|
return GetHash(hash.ComputeHash(input));
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-30 23:39:54 +00:00
|
|
|
|
|
2014-04-28 06:34:29 +00:00
|
|
|
|
private static string GetHash(byte[] bytes)
|
|
|
|
|
{
|
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var b in bytes)
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.Append(b.ToString("x2"));
|
2014-04-30 23:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stringBuilder.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|