Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

sha1sum outputs a hex encoded format of the actual sha. I would like to see a base64 encoded variant. possibly some command that outputs the binary version that I can pipe, like so: echo -n "message" | <some command> | base64 or if it outputs it directly that's fine too.

share|improve this question
add comment

4 Answers 4

up vote 9 down vote accepted

If you have the command line utility from OpenSSL, it can produce a digest in binary form, and it can even translate to base64 (in a separate invocation).

echo -n foo | openssl dgst -binary -sha1 | openssl base64
share|improve this answer
    
echo foo | openssl dgst -binary -sha1 | base64 is equivalent, and probably the cleanest way of doing this. –  xenoterracide Nov 3 '10 at 11:46
1  
Using openssl for base64 as well has the advantage of depending on only one tool (ksh: base64: not found). –  Gilles Nov 3 '10 at 19:51
add comment

Since sha1sum doesn't provide an option for binary output you'll likely need to find an utility which does the opposite of od and pipe them. Taking suggestion by fschmitt to use xxd with 'reverse' and 'plain dump' flags it will look like this:

sha1sum | cut -f1 -d\ | xxd -r -p | base64

share|improve this answer
3  
Use xxd with the -r-p flag. Like this: sha1sum somefile.txt | cut -f1 -d\ | xxd -r -p | base64 –  fschmitt Nov 1 '10 at 13:50
    
@fschmitt: I should have read this comment before posting my answer, it is much much cleaner. You should consider posting it as an answer. I'd vote for it. –  Steven D Nov 1 '10 at 17:30
    
It's more alex answer, I just googled "convert hex binary unix", so alex, feel free to edit your answer to include the xxd call and we'll vote this up. –  fschmitt Nov 1 '10 at 19:01
    
Edited my answer. Thanks for pointing xxd out :) –  alex Nov 2 '10 at 10:06
add comment

I'm not completely sure I understand what you want, but I think something like the following should work:

$ echo -ne "$(echo -n "message" | sha1sum | cut -f1 -d" " | sed -e 's/\(.\{2\}\)/\\x\1/g')" | base64

Basically, I take the hex output, use sed to make it a string of escaped hex values, and then use echo -en to echo the bytes into base64.

We can confirm that the final output corresponds to the same hash with the following exercise:

$ echo -n "message" | sha1sum 
6f9b9af3cd6e8b8a73c2cdced37fe9f59226e27d  -

$ echo -ne "$(echo -n "message" | sha1sum | cut -f1 -d" " | sed -e 's/\(.\{2\}\)/\\x\1/g')" | base64
b5ua881ui4pzws3O03/p9ZIm4n0=

$ echo -n "b5ua881ui4pzws3O03/p9ZIm4n0=" | base64 -d | xxd
0000000: 6f9b 9af3 cd6e 8b8a 73c2 cdce d37f e9f5  o....n..s.......
0000010: 9226 e27d                                .&.}

Visual inspection shows that our base64 value matches the original hex. Note that if you use hexdump rather than xxd you may have to play with the format settings a bit to get the output you expect.

share|improve this answer
add comment

Perl has a base64 module (in the base distribution since 5.7.1).

echo foo | sha1sum | \
perl -MMIME::Base64 -ne '/^([[:xdigit:]]+)/ and print encode_base64(pack("H*",$1))'

If you have the Digest::SHA module (in the base distribution since 5.9.3), or the older Digest::SHA1 module, you can do the whole computation in perl. As of perl 5.10.1, b64digest doesn't pad the base64 output; if you need padding the easiest way is to use MIME::Base64.

perl -MDigest::SHA -e 'print Digest::SHA->new(1)->addfile(*STDIN)->b64digest'
perl -MMIME::Base64 -MDigest::SHA \
     -le 'print encode_base64(Digest::SHA->new(1)->addfile(*STDIN)->digest)'
share|improve this answer
    
well if we're using perl, we could just use the Digest::SHA module which would allow us to output directly to base64. –  xenoterracide Nov 2 '10 at 0:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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