c# – SHA1CryptoServiceProvider and SHA1Managed return different results


I need to encrypt a string using SHA1. I was able to encrypt using the SHA1 class like this:

 public string CriptSha1(string secret)

            string result = "";

            byte[] key = System.Text.Encoding.UTF8.GetBytes(secret);

            SHA1 sha1 = SHA1Managed.Create();

            byte[] hash = sha1.ComputeHash(key);

            foreach (byte b in hash)
                result += b.ToString();

            return result;

If I happen to encrypt the word teste the output I have is:


Now if I encrypt using the SHA1CryptoServiceProvider class like this:

SHA1CryptoServiceProvider cryptTransformSHA1 = new SHA1CryptoServiceProvider();

byte[] buffer = Encoding.UTF8.GetBytes(secret);
string hashSecret = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-","");

The output I have is:


Can anyone tell me why? or what is the difference of these classes?


Your first code is incomplete, it just returns bytes and is not converting them.

This line:

result += b.ToString();

It should transform to hexadecimal, like this:

result += b.ToString("X2");


Ideone Example

Scroll to Top