I have a Python command that generates the URL-safe base-64 encoding of the SHA-1 hash of a string:
>>> import base64
>>> import sha
>>> base64.urlsafe_b64encode((sha.new("abc").digest()))
'qZk-NkcGgWq6PiVxeFDCbJzQ2J0='
I want to do the same thing in the bash shell, but I’m running into problems:
me:~$ echo -n "abc" | sha1sum | sed 's/ .*//'
a9993e364706816aba3e25717850c26c9cd0d89d
me:~$ echo -n "abc" | sha1sum | sed 's/ .*//' | base64
YTk5OTNlMzY0NzA2ODE2YWJhM2UyNTcxNzg1MGMyNmM5Y2QwZDg5ZAo=
I suspect that this is because of the way that sha1sum prints out (in hex format). I think base64 is reading 40 bytes, but it really should only be reading 20. I’ve tried piping it through iconv, but no success there:
me:~$ echo -n "abc" | sha1sum | sed 's/ .*//'
a9993e364706816aba3e25717850c26c9cd0d89d
me:~$ echo -n "abc" | sha1sum | sed 's/ .*//' | base64
YTk5OTNlMzY0NzA2ODE2YWJhM2UyNTcxNzg1MGMyNmM5Y2QwZDg5ZAo=
What should I be doing here to get the output I’m expecting?
Thanks in advance for the help!
digest() is not the same as hexdigest()
Docstring for digest:
EDIT: Try: