8

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
  • 745
  • 3
  • 9
  • 23

3 Answers3

11

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.

Jari Turkia
  • 647
  • 8
  • 13
3

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
  • 10,899
  • 2
  • 25
  • 24
2

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
Seyed M
  • 31
  • 2
  • 2
    Could you please [edit] your answer to give an explanation of why this code answers the question? Code-only answers are [discouraged](https://meta.stackexchange.com/questions/148272), because they don't teach the solution. – DavidPostill Jun 08 '21 at 12:13