Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

node.js - Is there a way to generate same Hmac hash output from PHP as done with nodejs?

Following is my nodejs function for creating HMAC hash -

function hash() {
    key = "hello";
    msg = "Hello";
    var hmac = crypto.createHmac('sha256', key);
    hmac.update(msg, 'utf8');
    var digest = hmac.digest();
    console.log('---hash =======>' + digest);
    return digest;
}

function hmacHex(key, msg) {
    var hmac = crypto.createHmac('sha256', key);
    hmac.update(msg, 'utf8');
    var digest = hmac.digest('hex');
    console.log('---hmacHex ====>' + digest);
    return digest;
}

var res = hash();
hmacHex("hello", res);

OUTPUT FROM hash() => }/?(K=?????('?Zt?h?D?Z??@yw??

OUTPUT FROM hmaxHex() => 692a18bd347476d28300e579794ba799cda80625191ef71783fce95692c2b6f9

Following is my PHP equivalent code for generating above mentioned output -

class Signature{

public function hash()
    {
        $key = "hello";
        $msg = "Hello";
        $hash = hash_hmac('sha256', utf8_encode($msg), $key, true);
        error_log('---hash ===>'.$hash);
        return $hash;
    }

public function hmacHex($msg)
    {
        $key = "hello";
        $hash = hash_hmac('sha256', utf8_encode($msg), $key, false);
        error_log('---hmacHex ===>' . $hash);
        return $hash;
    }
}

$obj = new Signature();

$res = $obj->hash();
$obj->hmacHex($res);
OUTPUT FROM hash() => }/?(K=?????('?Zt?h?D?Z??@yw??

OUTPUT FROM hmacHex => d920859abc579a070f2f8177e71d8431955784c8ed1d596b3364871ba35b5951

From the outputs, it is clear that in nodejs I am getting replacement characters whereas in PHP it appears as a question mark.

I am trying to generate exactly the same output from my PHP code so that when the outputs are rehashed it should be the same.

question from:https://stackoverflow.com/questions/65950232/is-there-a-way-to-generate-same-hmac-hash-output-from-php-as-done-with-nodejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'll update my answer to reflect your code changes. Thanks to @Topaco for resolving the issue.

php

class Signature {

    public function hash()
    {
        $key = "hello";
        $msg = "Hello";
        $hash = hash_hmac('sha256', $msg, $key, true);
        error_log('---hash ===>'.$hash);
        return $hash;
    }

    public function hmacHex($msg)
    {
        $key = "hello";
        $hash = hash_hmac('sha256', $msg, $key, false);
        error_log('---hmacHex ===>' . $hash);
        return $hash;
    }
}

$obj = new Signature();

$res = $obj->hash();
$obj->hmacHex($res);

node

class Signature {

    public function hash()
    {
        $key = "hello";
        $msg = "Hello";
        $hash = hash_hmac('sha256', $msg, $key, true);
        error_log('---hash ===>'.$hash);
        return $hash;
    }

    public function hmacHex($msg)
    {
        $key = "hello";
        $hash = hash_hmac('sha256', $msg, $key, false);
        error_log('---hmacHex ===>' . $hash);
        return $hash;
    }
}

$obj = new Signature();

$res = $obj->hash();
$obj->hmacHex($res);

php output:

---hash ===>}/(K=('ZthDZ@yw?
---hmacHex ===>692a18bd347476d28300e579794ba799cda80625191ef71783fce95692c2b6f9

Node.js output:

---hash =======>}/?(K=?????('?Zt?h?D?Z??@yw??
---hmacHex ====>692a18bd347476d28300e579794ba799cda80625191ef71783fce95692c2b6f9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...