Rami Sahoori
Rami Sahoori

Reputation: 85

How to get the hash only from command line?

When I try to SHA512 some file using openssl I got the output file contents starts with something like "SHA512(in.txt)= 090c..."

I tried the different options -r, -binary with the command

Here is the command I'm using openssl dgst -sha512 -out out.txt in.txt

The question is: How can I got the file contains only the hash, without that starting note?

Upvotes: 7

Views: 5213

Answers (3)

Raimundo Jimenez
Raimundo Jimenez

Reputation: 121

You could always obtain the hash in binary form and pipe it to xxd.

That way you do not have to worry about spaces in the filename.

openssl dgst -sha256 -binary in.txt | xxd -ps

To avoid the hash being split in different lines, you could combine -ps with -c 0 (the number of columns for the output) to obtain one single line of output from xxd...

openssl dgst -sha256 -binary in.txt | xxd -ps -c 0

Upvotes: 1

draxiom
draxiom

Reputation: 106

The default delimiter of awk is a space character, and the accepted answer will not work if there are spaces in the filename. You can override the default delimiter with the -F flag (field separator) to = , but that would also not work if there happens to be an equal space in the filename. Printing the last column using the default delimiter should work for all of those edge cases. The $NF awk variable stores the number of fields and can be used directly to print the last column, which should always be the hash.

openssl dgst -sha512 -out in.txt | awk '{print $NF}' > out.txt

https://linux.die.net/man/1/awk

Upvotes: 4

Thomas Mueller
Thomas Mueller

Reputation: 50097

You can only print the second column using awk, if the file name doesn't contain spaces:

openssl dgst -sha512 -out in.txt | awk '{print $2}' > out.txt

Or (looks like not cross-platform) you can try either pipe or reading from stdin:

openssl dgst -sha512 -out out.txt < in.txt
cat in.txt | openssl dgst -sha512 -out out.txt

This works for me (Mac OS X).

Upvotes: 4

Related Questions