Home > Software engineering >  Should SHA1CryptoServiceProvider and SHA256Managed return same value
Should SHA1CryptoServiceProvider and SHA256Managed return same value

Time:10-05

I've seen questions stating the same hash is generated, but with the following code:

    var password = "TestPassword";

    using (var hash = new SHA1CryptoServiceProvider())
    {
        var hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
        Console.WriteLine( Convert.ToBase64String(hashBytes) );
    }
    using (var managed = new SHA256Managed())
    {
        var hashBytes = managed.ComputeHash(Encoding.UTF8.GetBytes(password));
        Console.WriteLine(Convert.ToBase64String(hashBytes));
    }

The output is:

YlBiWyJt9ihwriOvjT sB2DXFYg=
e8 diSmPG/rhb6Au1rYZCP0vqN5F3Y4hU6PEcwB2Uyg=

Am I not using the crypto libraries correctly?

CodePudding user response:

SHA1 and SHA256 are two different hash functions. (Source: https://en.wikipedia.org/wiki/Secure_Hash_Algorithms)

  • Related