9

I need to base64 encode a binary sha1 hash. What is the best way to do this? I imagine this would involve piping binary data into base64. Unfortunately sha1sum does not have a binary output option. Any ideas?

This is what I need to do:

echo mydata|sha1sum --binary-output|base64

sha1sum does not have a --binary-output option though.

jcalfee314
  • 765
  • 4
  • 10
  • 23

3 Answers3

15

Maybe something like:

echo mydata | sha1sum | xxd -r -p | base64

... would solve your problem.

See https://unix.stackexchange.com/questions/82561/convert-a-hex-string-to-binary-and-send-with-netcat for similar question.

6

You can use the openssl to CalculateSHA-1 and -binary output the digest in binary form.

Last piping convert binary-digest to Base-64.

echo -n mydata | openssl dgst -binary -sha1 | openssl base64

Explanation

Sending data (mydata in this case) to STDOUT:
echo -n mydata

-n flag remove new line (\n) from output.

Calculating SHA-1 via OpenSSL:
 openssl dgst -sha1

However, this command displays the SHA-1 in HEX mode, and for binary, the -binary flag needs to be added:

openssl dgst -binary -sha1
Convert SHA-1 to Base-64:
openssl base64
Seyed M
  • 71
4

Try converting hex to base64. This answer is one option. There are a number of other implementations.

How can I convert from hex to base64?

BillThor
  • 11,345
  • 2
  • 28
  • 25