盐和哈希方法 [英] salt and hash method
问题描述
好。我的盐值是????我不知道为什么这里是盐和哈希的方法
盐方法
UnicodeEncoding utf16 = new UnicodeEncoding();
if(utf16!= null)
{
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
if(random!= null)
{
byte [] saltValue = new byte [8];
random.NextBytes (saltValue);
string saltValueString = utf16.GetString(saltValue);
哈希方法
UnicodeEncoding encoding = new UnicodeEncoding();
SHA512Managed hashing = new SHA512Managed();
string salt = string.Empty;
if(tb_password.Text!= null && hashing!= null && encoding != null)
{
if(salt == null)
{
//生成一个盐串。
salt = encoding.GetStrin g(saltValue);
}
byte [] binarySaltValue = new byte [8];
binarySaltValue [0] = byte.Parse(salt.Substring(0,2),System.Globalization.NumberStyles.HexNumber,CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue [1] = byte.Parse(salt .Substring(2,2),System.Globalization.NumberStyles.HexNumber,CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue [2] = byte.Parse(salt.Substring(4,2),System .Globalization.NumberStyles.HexNumber,CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue [3] = byte.Parse(salt.Substring(6,2),System.Globalization.NumberStyles.HexNumber,CultureInfo。 InvariantCulture.NumberFormat);
byte [] valueToHash = new byte [8 + encoding.GetByteCount(tb_password.Text)];
byte [] binaryPassword = encoding.GetBytes(tb_password.Text);
binarySaltValue.CopyTo(valueToHash,0);
binaryPassword.CopyTo(valueToHash,8); < br $>
byte [] hashValue = hashing.ComputeHash(valueToHash);
string hashedPassword = salt;
foreach(hashValue中的字节hexdigit)
{
hashedPassword + = hexdigit.ToString(X2,CultureInfo .InvariantCulture.NumberFormat);
}
Okay. My salt value is ???? i not sure why here is my method for the salt and hash
Salt method
UnicodeEncoding utf16 = new UnicodeEncoding();
if (utf16 != null)
{
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
if (random != null)
{
byte[] saltValue = new byte[8];
random.NextBytes(saltValue);
string saltValueString = utf16.GetString(saltValue);
Hash method
UnicodeEncoding encoding = new UnicodeEncoding();
SHA512Managed hashing = new SHA512Managed();
string salt = string.Empty;
if (tb_password.Text != null && hashing != null && encoding != null)
{
if (salt == null)
{
// Generate a salt string.
salt = encoding.GetString(saltValue);
}
byte[] binarySaltValue = new byte[8];
binarySaltValue[0] = byte.Parse(salt.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[1] = byte.Parse(salt.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[2] = byte.Parse(salt.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[3] = byte.Parse(salt.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
byte[] valueToHash = new byte[8 + encoding.GetByteCount(tb_password.Text)];
byte[] binaryPassword = encoding.GetBytes(tb_password.Text);
binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, 8);
byte[] hashValue = hashing.ComputeHash(valueToHash);
string hashedPassword = salt;
foreach (byte hexdigit in hashValue)
{
hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
}
推荐答案
阅读简单安全密码验证说明 [ ^ ]。
这篇关于盐和哈希方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!