Closed jbirnick closed 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.
Thanks, but this also applies for files. How to fix it for files? I tried -z
but it doesn't help.
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.
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.
$ 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 -
Yes echo -n X
is the same as printf %s X
, except non-standard.
When I do
echo 'asdf' | keccak-256sum
I getbut various online tools say the keccak-256 hash of
asdf
isSame thing for the other hash functions. What's going on?