user278618
user278618

Reputation: 20242

SHA1 hash question

I have this method to hash a string:

 byte[] buffer = enc.GetBytes(text);
 SHA1CryptoServiceProvider cryptoTransformSHA1 =
                new SHA1CryptoServiceProvider();
 string hash = BitConverter.ToString(
                cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

 return hash;

My question is:

Is the resulting hash always the same for the same string?

I hashed a string a couple of days ago and it seems that it now resulted in another hash, but i'm not sure.

Upvotes: 4

Views: 1712

Answers (3)

Pavel Nikolov
Pavel Nikolov

Reputation: 9541

Id depends! The same plain text string will hash to the same SHA1 hash if you use the same Encoding! Using different Encoding will result in different SHA1 hash.

Upvotes: 0

Michal Ciechan
Michal Ciechan

Reputation: 13888

As long as the bytes are the same, you will end up with exactly the same hash. Note that special characters and whitespace are bytes as well.

Wikipedia Link

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

Yes, the same plaintext string will hash to the same SHA1 hash every time.

Upvotes: 10

Related Questions