maandree / sha3sum

[Feature complete] SHA-3 and Keccak checksum utility
https://codeberg.org/maandree/sha3sum
ISC License
174 stars 51 forks source link

Wrong hashes? #31

Closed jbirnick closed 3 years ago

jbirnick commented 3 years ago

When I do echo 'asdf' | keccak-256sum I get

30a730f7c2718bbcfa3dc40523a288a6d5f802c1ef75116eb0f42b241318a159

but various online tools say the keccak-256 hash of asdf is

4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64

Same thing for the other hash functions. What's going on?

maandree commented 3 years ago
$ printf '%s\n' 'asdf' | keccak-256sum
30a730f7c2718bbcfa3dc40523a288a6d5f802c1ef75116eb0f42b241318a159  -
$ printf '%s' 'asdf' | keccak-256sum
4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64  -

echo(1) (not all implementations, I don't think Mac's does) adds an LF at the end.

jbirnick commented 3 years ago

Thanks, but this also applies for files. How to fix it for files? I tried -z but it doesn't help.

maandree commented 3 years ago

Normally text files (or actually per POSIX definition) end with an LF, the sha3sum utilities does not remove this, I assume the other tools you have tested for some reason does. You should be able to remove it, and get the same result from all implementations. A simple way to do this in the shall to run tr '\n\0' '\0\n' < file | sed '$s/\x00$//' | tr '\n\0' \0\n' | keccak-256sum.

-z is an option for -c which allows you to have filenames containing LF, that option is not relevant in this situation.

maandree commented 3 years ago

Maybe, the other tools are in text-mode rather than binary mode (sha3sum does not have this because similar tools only Linux ignore the text/binary mode options). If so, substituting CR LF and LF each other in the files may not change the checksum either.

jbirnick commented 3 years ago
$ printf '%s\n' 'asdf' | keccak-256sum
30a730f7c2718bbcfa3dc40523a288a6d5f802c1ef75116eb0f42b241318a159  -
$ printf '%s' 'asdf' | keccak-256sum
4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64  -

For others: It's also possible to use echo -n, which omits the trailing newline.

$ echo -n 'asdf' | keccak-256sum
4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64  -
maandree commented 3 years ago

Yes echo -n X is the same as printf %s X, except non-standard.