HMAC-SHA1: How to do it properly in Java? HMAC-SHA1: How to do it properly in Java? java java

HMAC-SHA1: How to do it properly in Java?


On your PHP side, use single-quotes around the key so that the $ character is not treated as a variable reference. i.e.,

hash_hmac("sha1", "helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo')

Otherwise, the key you really get is PRIE7-Yf17kEnUEpi5hvW/#AFo (assuming the variable $oG2uS is not defined).


Recommend Apache Common Codec Library, quite simple and easy to use.HmacUtils.hmacSha1Hex(key, string_to_sign);


Any $ symbol in double quotes ("") is regarded as a a variable in PHP. You can avoid the error by using either single quotes as pointed out by the previous commenter or you can escape the dollar sign as below

hash_hmac("sha1", "helloworld", "PRIE7\$oG2uS-Yf17kEnUEpi5hvW/#AFo")

Notice $ is now \$