class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sergio");
Console.WriteLine(HashString("Sergio"));
Console.WriteLine(HashString("Sergio"));
Console.WriteLine(HashString("Sergio"));
Console.ReadKey();
}
public static string HashString(string value)
{
int minSaltSize = 4;
int maxSaltSize = 8;
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);
byte[] saltBytes = new byte[saltSize];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(saltBytes);
//Convert the string value into a byte array.
UTF8Encoding utf8Encoder = new UTF8Encoding();
byte[] plainTextBytes = utf8Encoder.GetBytes(value);
//Allocate an array to hold the text bytes and the salt bytes.
byte[] plainTextWithSaltBytes = new Byte[saltBytes.Length + plainTextBytes.Length];
for (int i = 0; i < plainTextBytes.Length; i++)
{
plainTextWithSaltBytes[i] = plainTextBytes[i];
}
for (int i = 0; i < saltBytes.Length; i++)
{
plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
}
HashAlgorithm hash = new SHA256Managed();
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
string hashedValue = Convert.ToBase64String(hashBytes);
return hashedValue;
}
}
내 코드의 결과는 해시 된 "Sergio"가 항상 다릅니다. 이것은 소금에 포함 된 임의의 요인에 의해 발생합니다. 내 질문은 내가 소금을 어딘가에 저장해야한다는거야? 아니면 내 코드에 상수를 정의합니까?암호를 해싱 할 때 소금을 어딘가에 저장해야합니까?
감사합니다.
안녕하세요, 나는 당신이 그것을 알아야하거나 당신이 그것을 저장해야하거나 당신이 어딘가에 저장된 고정 값에 의존해야한다고 말하고 싶습니다. –
그러한 코드를 직접 작성하면 안됩니다. 전문가조차도이를 망쳐 놓는다. 이미 검증 된 상용 인증 공급자를 사용하십시오. 바퀴의 재발견은 일반적으로 소프트웨어에서는 좋지 않지만 치명적일 수 있습니다. 귀하의 앱이 다음 Gawker가되지 않도록하십시오! –
@Craig : 하하하, 사실. 그러나 나는 항상 사물이 어떻게 작동하는지, 심지어 표면적 인 수준에서도 배우는 것을 좋아합니다. 어느 쪽이든 upvote. –