简体   繁体   中英

SHA256 in C# on Windows vs. openssl on unix

I am computing a hash on unix with the following:

echo -n "Test123" | openssl dgst -binary -sha256 | openssl base64

This gives:

9JKwVu6ULKgdcQq8I2QugjqDhkvHgiqq8bsBtM7PNGc=

I have the following C# code:

string enc;
using(SHA256Managed sha = new SHA256Managed())
{
    enc = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes("Test123")));
}

And this gives:

2bX1jws4GYKTIxhIoUB09Z66PoJZQ+y+hq5R8dnx9I4=

I do not understand why these are not identical?

I knew this had to be something stupid and it was. On the problematic box I was logged in as root so I was on a K-shell. From there it doesn't handle the echo -n the same way as from a C-shell. Leaving this here because I will probably have this problem again in five years :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM