Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA1 hash question

Tags:

c#

sha1

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.

like image 238
user278618 Avatar asked Dec 13 '22 23:12

user278618


2 Answers

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

like image 196
Ignacio Vazquez-Abrams Avatar answered Dec 15 '22 12:12

Ignacio Vazquez-Abrams


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

like image 43
Michal Ciechan Avatar answered Dec 15 '22 13:12

Michal Ciechan