Translate HmacSHA1 checksum generating method from Java to PHP

134 views Asked by At

I would like to translate the following Java method to PHP:

    private byte[] generateChecksum(byte[] inData){
        try{
            byte[] b_key = secretValue.getBytes("ISO-8859-1");

            SecretKeySpec sha1Key = new SecretKeySpec(b_key, "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(sha1Key);

            byte[] sigBytes = mac.doFinal(inData);
            return sigBytes;

        }catch(Exception e){

            log.error("problem create hash: " + e);
            return null;
        }
    }

I came up with the following, but it doesn't seem to be producing the same results as the Java method:

private function generateChecksum ( $inData ) {

    try {

        $hash = hash_hmac( 'sha1', $inData, $this->secretValue, TRUE );

        return $this->getBytes( $hash );

    } catch (Exception $e) {

        return null;
    }

}
1

There are 1 answers

0
EnoTy On

I resolved it. The issue was caused by passing the $inData to hash_hmac( ) as a byte array instead of a string.