DSInternals/Src/DSInternals.Common/Cryptography/OrgIdHash.cs

73 lines
2.3 KiB
C#
Raw Normal View History

2016-02-22 22:19:34 +00:00
using DSInternals.Common;
2015-12-26 22:44:43 +00:00
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace DSInternals.Common.Cryptography
{
public static class OrgIdHash
{
public const int SaltSize = 10;
/// <summary>
/// The size, in bytes, of the computed hash code.
/// </summary>
public const int HashSize = 32;
2016-11-08 21:21:51 +00:00
private const int Iterations = 1000;
2015-12-26 22:44:43 +00:00
private const string HashFormat = "v1;PPH1_MD4,{0},{1},{2};";
2016-02-22 22:19:34 +00:00
private const string InternalHashFunction = "HMACSHA256";
2015-12-26 22:44:43 +00:00
public static byte[] GenerateSalt()
{
using(var rng = new RNGCryptoServiceProvider())
{
byte[] salt = new byte[SaltSize];
rng.GetBytes(salt);
return salt;
}
}
public static byte[] ComputeHash(SecureString password, byte[] salt)
{
byte[] ntHash = NTHash.ComputeHash(password);
return ComputeHash(ntHash, salt);
}
public static byte[] ComputeHash(byte[] ntHash, byte[] salt)
{
Validator.AssertLength(ntHash, NTHash.HashSize, "ntHash");
Validator.AssertLength(salt, SaltSize, "salt");
string hexHash = ntHash.ToHex(true);
byte[] hexHashBytes = UnicodeEncoding.Unicode.GetBytes(hexHash);
2016-02-22 22:19:34 +00:00
var pbkdf2 = new Pbkdf2(hexHashBytes, salt, Iterations, InternalHashFunction);
byte[] orgIdHashBytes = pbkdf2.GetBytes(HashSize);
2015-12-26 22:44:43 +00:00
return orgIdHashBytes;
}
public static string ComputeFormattedHash(SecureString password, byte[] salt = null)
{
byte[] ntHash = NTHash.ComputeHash(password);
return ComputeFormattedHash(ntHash, salt);
}
public static string ComputeFormattedHash(byte[] ntHash, byte[] salt = null)
{
if (salt == null)
{
salt = GenerateSalt();
}
else
{
Validator.AssertLength(salt, SaltSize, "salt");
}
byte[] hash = ComputeHash(ntHash, salt);
return FormatHash(hash, salt);
}
private static string FormatHash(byte[] hash, byte[] salt)
{
return string.Format(HashFormat, salt.ToHex(false), Iterations, hash.ToHex(false));
}
}
}