问题
I am using openssl 1.0.1c , linux x86_64
i am creating file contains "hello" (without new line character)
- openssl dgst -sha256 hello_file
i get : 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
If i am using any other online calc (1 , 2 , 3 , 4 , 5 (because of lack of reputation i cant do more hyperlinks)
i get : 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
The most confusing part is if i am trying online calculator with "hello(new line character)" then
1 return : 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
and it is exactly what i am getting with openssl. While all others return
2, 3, 4 , 5 : cd2eca3535741f27a8ae40c31b0c41d4057a7a7b912b33b9aed86485d1c84676
I knew about new line issue with echo, but i didn't knew that each file appended with the new line.So how can i get sha256 of file without new line character ? And what is wrong with all other calculators ?
Thanks in advance
Kirill
3 convertstring.com/Hash/SHA256
4 webutils.pl/index.php?idx=sha1
5 quickhash.com
回答1:
You are correct, your hello_file
has a newline:
$ echo 'hello' > hello_file; openssl sha256 hello_file; xxd hello_file
SHA256(hello_file)= 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
0000000: 6865 6c6c 6f0a hello.
Removing the new line would depend on your editor, if your situation is similar to mine when i found this post, messing with a hash of a password you can just do something like this:
$ echo -n 'hello' > hello_file; openssl sha256 hello_file; xxd hello_file
SHA256(hello_file)= 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
0000000: 6865 6c6c 6f hello
You can do something like this to remove the last character(or two):
$ echo "hello" > hello_file; od -c hello_file; truncate hello_file --size=-1; od -c hello_file
0000000 h e l l o \n
0000006
0000000 h e l l o
0000005
It looks as if the other calculators are appending a dos style carriage return + new line
$ echo -en 'hello\r\n' > hello_file; openssl sha256 hello_file; xxd hello_file
SHA256(hello_file)= cd2eca3535741f27a8ae40c31b0c41d4057a7a7b912b33b9aed86485d1c84676
0000000: 6865 6c6c 6f0d 0a hello..
来源:https://stackoverflow.com/questions/11742460/openssl-sha256-diff