为什么我没有让sha256正确?

use*_*358 0 c++ hash stdout sha256 gnu-coreutils

我正在使用crypto ++库进行一些练习.我期待与从shell调用的sha256sum工具相同的输出.

// typedef unsigned char byte;
byte out[CryptoPP::SHA256::DIGESTSIZE];
byte in=65;  // 'A'
CryptoPP::SHA256().CalculateDigest(out, &in, 1);
for(int i=0; i < CryptoPP::SHA256::DIGESTSIZE; i++)
    std::cout << std::hex << (int)out[i];
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

559aead08264d5795d399718cdd5abd49572e84fe55590eef31a88a08fdffd

$ echo A | sha256sum
Run Code Online (Sandbox Code Playgroud)

06f961b802bc46ee168555f066d28f4f0e9afdf3f88174c1ee6f9de004fc30a0

为什么两者不相等?

Ble*_*der 7

echo附加换行符,因此您要比较不同字符串的哈希值.printf改为使用:

$ printf 'A' | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -
$ printf 'A\n' | sha256sum
06f961b802bc46ee168555f066d28f4f0e9afdf3f88174c1ee6f9de004fc30a0  - 
Run Code Online (Sandbox Code Playgroud)


Joa*_*son 6

在命令行中,您正在校验和A+换行.

如果您的unix版本支持它,请使用echo -necho而不添加换行符;

$ echo -n A | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -
Run Code Online (Sandbox Code Playgroud)