3

I have a 32 byte key stored in a file called test_key.key

I wish to use openssl to create hash of a different file called mytext.txt. I need to create the hash using HMAC-SHA-256 & the key in test_key.key

I thought I had it working with the following command:

openssl dgst -sha256 -hmac $test_key.key mytext.txt

However, on closer examination I noticed it was using the string "test_key.key", not the actual file contents. I have tried a few different variations of this for the key file such as:

"$test_key.key"
'$test_key.key'
/test_key.key

Sadly, they all seem to have the same problem.

Can anyone advise me on this? I wish to keep this as a one line command if possible.

4
  • 3
    openssl dgst -sha256 -hmac $(<test_key.key) mytext.txt Commented May 23, 2018 at 8:11
  • 1
    @DavidC.Rankin I'm surprised that this works, especially if the key contains non printable character. Personally I prefer openssl dgst -sha256 -mac HMAC -macopt "hexkey:$(xxd -ps -c32 test_key.key)" mytext.txt that is explicit about the key format. Commented May 23, 2018 at 8:34
  • There are quite few caveats to the general use, but with a sha256 string of unsigned char you should be fine (as long as there isn't a '/* ' or something else that could be interpreted as a independent command) I wouldn't recommend it for much else. Commented May 23, 2018 at 8:41
  • 1
    @DavidC.Rankin calling openssl ... $(<test_key.key) ... exposes the key in the process info Commented Jul 12, 2022 at 9:15

1 Answer 1

5
openssl dgst -sha256 -hmac $(<test_key.key) mytext.txt 

(credit to user David C. Rankin)

Sign up to request clarification or add additional context in comments.

1 Comment

It is helpful to use quotes to avoid issues with special characters (couldn't edit because edits must have 6 chars): openssl dgst -sha256 -hmac "$(<test_key.key)" mytext.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.