Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我没有弄对sha256?_C++_Hash_Stdout_Sha256_Gnu Coreutils - Fatal编程技术网

C++ 为什么我没有弄对sha256?

C++ 为什么我没有弄对sha256?,c++,hash,stdout,sha256,gnu-coreutils,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++

我正在使用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;
06F961B802BC46EE168555F066D28F4F0E9AFDFF3F88174C1EE6F9DE004FC30A0


为什么两者不相等?

echo
追加了一个换行符,因此您正在比较不同字符串的哈希值。改用
printf

$ printf 'A' | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -
$ printf 'A\n' | sha256sum
06f961b802bc46ee168555f066d28f4f0e9afdf3f88174c1ee6f9de004fc30a0  - 

在命令行中,对
A
+行馈送进行校验和

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

$ echo -n A | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -
$ echo -n A | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -