james
james

Reputation: 170

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. 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:

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

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.

Upvotes: 2

Views: 12730

Answers (3)

Manuel Alves
Manuel Alves

Reputation: 4013

Please see tutorial here: 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"

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108790

Hashing with plain SHA-512 is still wrong. Use PBKDF2 which is exposed via Rfc2898DeriveBytes.

It returns raw bytes, which you should encode with either hex or base64.

You can do hex encoding with:

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

Upvotes: 5

godzcheater
godzcheater

Reputation: 464

You sure it said 512 because that's 128, but anyway you could use something like

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.

Upvotes: 1

Related Questions