Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSSL HMCA SHA1 not giving expected result

I'm trying to use OpenSSL to generate a checksum in CMD, as per the top answer here.

However, using the provided example I get an unexpected result:

C:\>echo -n "value" | openssl dgst -sha1 -hmac "key"
(stdin)= 8c5b4c3a9cee7bc9020a43f1c396f9e13c2bae4a

The expected result as shown in the original question, which I also get with other HMAC SHA1 generators is:

57443a4c052350a44638835d64fd66822f813319

Curiously, I get a third result in PowerShell:

PS C:\> echo -n "value" | openssl sha1 -hmac "key"
(stdin)= 56d96e5393d98eb5e189ab189e02b1832af727b5

As might be self evident, I'm a bit out of my comfort zone here, so forgive me for any obvious mistakes or shortcomings in my explanation.

like image 321
Alex Avatar asked Sep 16 '25 14:09

Alex


1 Answers

Scraping together answers to several questions on SO teaches some tricks to get the same result for all three cases:

In *nix-like environments (including macOS), printf is a more portable way to print without a newline:

$ printf value | openssl dgst -sha1 -hmac key
57443a4c052350a44638835d64fd66822f813319

A trick to avoid the newline in CMD (note that there is no space before the second |, this is essential):

>echo | set /p=value| openssl dgst -sha1 -hmac key
(stdin)= 57443a4c052350a44638835d64fd66822f813319

With PowerShell this does not seem possible "natively" at the moment, according to this issue in the PowerShell GitHub project: Piping Text To An External Program Appends A Trailing Newline. If you really have to do it from a PowerShell prompt, a hack could be to invoke CMD, like this:

> cmd /c "echo | set /p=value| openssl dgst -sha1 -hmac key"
(stdin)= 57443a4c052350a44638835d64fd66822f813319
like image 158
Reinier Torenbeek Avatar answered Sep 18 '25 09:09

Reinier Torenbeek