Programming
php bash hash openssl sign
Updated Tue, 19 Jul 2022 20:41:34 GMT

PHP equivalent of openssl dgst -sha1 -hmac KEY -binary


I try to implement this bash command in PHP without success :

password='echo -en "$date" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64'

I tried the openssl_digest and openssl_encrypt without success. I don't understand the order of the parameters...

Could you help me to generate the expected command in PHP ?

Thanks for your help !




Solution

The bash command :

password='echo -en "$date" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64'

can be interpreted in PHP as follows :

$password = base64_encode(hash_hmac('sha1', $date, $apiKey, true));