简体   繁体   English

如何使用SHA512哈希密码

[英]How to hash a password with SHA512

In my previous question I was told to hash passwords instead of encrypt, and that turned out to be correct. 在我之前的问题中,我被告知要对密码进行散列而不是加密,事实证明这是正确的。 Problem is, I've never dealt with hashing passwords before and all the docs say SHA512 which I've tried to use on a test account to no avail. 问题是,我之前从未处理过哈希密码,并且所有文档都说SHA512,我曾尝试在测试帐户上使用它,但无济于事。 I'm not sure where to go from here. 我不知道从哪里开始。 The code comments give me the example "encrypted" string as they call it, and it's "FA35A0194E3BE7024CEFB1839CBFC922" which I'm not sure how to format it like that with SHA512 since all it takes and gives back is a byte array or stream from the ComputeHash() method: 代码注释给了我一个示例“加密的”字符串,正如他们所说的那样,它是“ FA35A0194E3BE7024CEFB1839CBFC922”,我不确定如何使用SHA512对其进行格式化,因为它所需要的并返回的是字节数组或来自ComputeHash()方法:

byte[] hashedPassword = HashAlgorithm.Create("SHA512").ComputeHash( ??? );

UPDATE UPDATE

I've tried printing out the UTF8Encoding.GetString on the bytes, but it just displays a bunch of bullshit characters that look nothing like the one in the example docs. 我尝试在字节上打印出UTF8Encoding.GetString,但它只显示了一堆废话字符,看起来与示例文档中的字符不一样。

Hashing with plain SHA-512 is still wrong. 使用普通的SHA-512进行哈希处理仍然是错误的。 Use PBKDF2 which is exposed via Rfc2898DeriveBytes . 使用通过Rfc2898DeriveBytes公开的PBKDF2。

It returns raw bytes, which you should encode with either hex or base64. 它返回原始字节,您应使用十六进制或base64对其进行编码。

You can do hex encoding with: 您可以使用以下方式进行十六进制编码:

BitConverter.ToString(bytes).Replace("-","")

You sure it said 512 because that's 128, but anyway you could use something like 您确定它说的是512,因为那是128,但是无论如何您都可以使用类似

System.String Hashed = System.BitConverter.ToString(((System.Security.Cryptography.SHA512)new System.Security.Cryptography.SHA512Managed()).ComputeHash(System.Text.Encoding.ASCII.GetBytes("NotHashedPass"))).Replace("-","");

MessageBox.Show(Hashed);

but id recommend at least using a salt. 但ID建议至少使用盐。

Please see tutorial here: http://www.obviex.com/samples/hash.aspx 请在此处查看教程: http : //www.obviex.com/samples/hash.aspx

From the tutorial: "These code samples demonstrate how to hash data and verify hashes. It supports several hashing algorithms. To help reduce the risk of dictionary attacks, the code prepends random bytes (so-called salt) to the original plain text before generating hashes and appends them to the generated ciphertext (original salt value will be needed for hash verification). The resulting ciphertext is base64-encoded. IMPORTANT: DATA HASHES CANNOT BE DECRYPTED BACK TO PLAIN TEXT" 在本教程中,“这些代码示例演示了如何对数据进行哈希处理和验证哈希。它支持多种哈希算法。为帮助减少字典攻击的风险,代码在生成之前将随机字节(所谓的salt)添加到原始纯文本之前哈希并将其附加到生成的密文中(哈希验证需要原始盐值。生成的密文是base64编码的。重要信息:无法将数据哈希解密回纯文本”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM