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.

  • 3
    How did binary creep into this?
    – tink
    May 30, 2013 at 23:32
  • 2
    I believe that jcalfee314 is saying that he wants to base64 encode an actual SHA1 hash value, rather than the hex representation thereof that the sha1 program outputs. May 30, 2013 at 23:47

3 Answers 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?


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.

  • 2
    Beautiful! This also works flawlessly under macOS Big Sur Beta (in late 2020). Oct 23, 2020 at 19:00

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
  • 1
    Could you please edit your answer to give an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.
    – DavidPostill
    Jun 8, 2021 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.