I'd like to compute a HMAC-SHA512 digest in my bash script. So far I've found only this repeated many times over many different sites.
echo -n message | openssl dgst -sha256 -hmac secret -binary >message.mac
Apparently no one posting this realizes this is not the proper way to pass a secret string to a program as the secret will be visible in the process list for every other process running on the system. Is there any other way (perhaps with other tool) to easily make an HMAC in the shell with better interface for passing secrets?
UPDATE
I use the following tool (~/bin/hmac
) now. It takes the key from the MACKEY
environment variable.
#!/usr/bin/env python3
import hmac, sys, os
key = os.environ['MACKEY'].encode('utf-8')
algo = os.getenv('MACALGO', 'sha512')
digest = hmac.new(key, digestmod = algo)
while True:
buf = sys.stdin.buffer.read(512)
if not buf:
break
digest.update(buf)
print(digest.hexdigest())
Usage:
echo -n message | MACKEY=foobar hmac
-passin
would support envvars or files, but I can't see anything like that for a MAC key... And no,-passin
itself doesn't work. – ilkkachu Jan 26 '18 at 13:02/proc
withhidepid=2
:mount -o remount,rw,hidepid=2 /proc
- don't forget to update fstab if you want /proc persistently mounted like that:proc /proc proc defaults,hidepid=2 0 0
. NB: this will obviously not be useful in your situation if you're trying to hide the secret entirely from ps/top outputs for all users. – RobotJohnny Jan 26 '18 at 13:15cat keys | { read api_key; read api_secret }
. – Mark Howard Jan 26 '18 at 13:35