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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With