May 21, 2014 Archives

21-05-2014 13:32

How to generate a HmacSHA512 from the shell

This is ripped right from here but I am copying here for my own (and others) reference.

I realise this isn't exactly what you're asking for, but there's no point in reinventing the wheel and writing a bash version. You can simply use the openssl command to generate the hash within your script.

[me@home] echo -n "value" | openssl dgst -sha1 -hmac "key"
57443a4c052350a44638835d64fd66822f813319
Or simply:
[me@home] echo -n "value" | openssl sha1 -hmac "key"
57443a4c052350a44638835d64fd66822f813319
Remember to use -n with echo or else a line break character is appended to the string and that changes your data and the hash. That command comes from the OpenSSL package which should already be installed (or easily installed) in your choice of Linux/Unix, Cygwin and the likes. Do note that older versions of openssl (such as that shipped with RHEL4) may not provide the -hmac option. As an alternative solution, but mainly to prove that the results are the same, we can also call PHP's hmac_sha1() from the command line:
[me@home]$ echo '' | php
57443a4c052350a44638835d64fd66822f813319
Edit: One could use printf rather than echo -n.


Posted by DaveQB | Permanent Link | Categories: IT