Question

I am using crypto++ library for some exercise. I am expecting the same output as from the sha256sum tool invoked from the shell.

// 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;

559aead08264d5795d399718cdd5abd49572e84fe55590eef31a88a08fdffd

$ echo A | sha256sum

06f961b802bc46ee168555f066d28f4f0e9afdf3f88174c1ee6f9de004fc30a0

Why are the two not equal?

Was it helpful?

Solution

echo appends a newline, so you're comparing the hashes of different strings. Use printf instead:

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

OTHER TIPS

In your command line, you're checksumming A + a line feed.

If your unix version supports it, use echo -n to echo without adding the line feed;

$ echo -n A | sha256sum
559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd  -
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top