最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - how to get Hmac code with javascript - Stack Overflow

programmeradmin2浏览0评论

I can get a hmac sing using Python as following:

import hmac, base64, hashlib

def make_sign():
    hash_data = "data"
    secret = "this is secret"
    sha512 = hashlib.sha512
    hmac_obj = hmac.new(secret, hash_data, sha512)
    str_hash = hmac_obj.digest()
    sign = base64.b64encode(str_hash)
    hex_hash = hmac_obj.hexdigest()
    hex_sign = base64.b64encode(hex_hash)
    print "correct_sign:",sign
    print "hex_digest_sign:",hex_sign
make_sign()

output:

correct_sign: Lg4pXNCIpitNQt2DLU19qWb+FxdsYZlK4LLncfkTzSidrYoFJLNolUziRqh09B5HyRdCTEP7enZp6/Te34FK1g==
hex_digest_sign: MmUwZTI5NWNkMDg4YTYyYjRkNDJkZDgzMmQ0ZDdkYTk2NmZlMTcxNzZjNjE5OTRhZTBiMmU3NzFmOTEzY2QyODlkYWQ4YTA1MjRiMzY4OTU0Y2UyNDZhODc0ZjQxZTQ3YzkxNzQyNGM0M2ZiN2E3NjY5ZWJmNGRlZGY4MTRhZDY=

but with js, I can get hex_digest_sign, but I need to get correct_sign for web request.

function make_request() {
    hash_data = "data"
    secret = "this is secret"
    hmac = hmac_512(hash_data, secret)
    var sign = $.base64.encode(hmac),
    console.log("js_sign="+sign);
}

function hmac_512(message, secret) {
    var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, secret);
    hmac.update(message);
    var hash = hmac.finalize();
    return hash;
}

js output:

js_sign="MmUwZTI5NWNkMDg4YTYyYjRkNDJkZDgzMmQ0ZDdkYTk2NmZlMTcxNzZjNjE5OTRhZTBiMmU3NzFmOTEzY2QyODlkYWQ4YTA1MjRiMzY4OTU0Y2UyNDZhODc0ZjQxZTQ3YzkxNzQyNGM0M2ZiN2E3NjY5ZWJmNGRlZGY4MTRhZDY="

the correct sign is

correct_sign: Lg4pXNCIpitNQt2DLU19qWb+FxdsYZlK4LLncfkTzSidrYoFJLNolUziRqh09B5HyRdCTEP7enZp6/Te34FK1g==

how to get it in js?

I can get a hmac sing using Python as following:

import hmac, base64, hashlib

def make_sign():
    hash_data = "data"
    secret = "this is secret"
    sha512 = hashlib.sha512
    hmac_obj = hmac.new(secret, hash_data, sha512)
    str_hash = hmac_obj.digest()
    sign = base64.b64encode(str_hash)
    hex_hash = hmac_obj.hexdigest()
    hex_sign = base64.b64encode(hex_hash)
    print "correct_sign:",sign
    print "hex_digest_sign:",hex_sign
make_sign()

output:

correct_sign: Lg4pXNCIpitNQt2DLU19qWb+FxdsYZlK4LLncfkTzSidrYoFJLNolUziRqh09B5HyRdCTEP7enZp6/Te34FK1g==
hex_digest_sign: MmUwZTI5NWNkMDg4YTYyYjRkNDJkZDgzMmQ0ZDdkYTk2NmZlMTcxNzZjNjE5OTRhZTBiMmU3NzFmOTEzY2QyODlkYWQ4YTA1MjRiMzY4OTU0Y2UyNDZhODc0ZjQxZTQ3YzkxNzQyNGM0M2ZiN2E3NjY5ZWJmNGRlZGY4MTRhZDY=

but with js, I can get hex_digest_sign, but I need to get correct_sign for web request.

function make_request() {
    hash_data = "data"
    secret = "this is secret"
    hmac = hmac_512(hash_data, secret)
    var sign = $.base64.encode(hmac),
    console.log("js_sign="+sign);
}

function hmac_512(message, secret) {
    var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, secret);
    hmac.update(message);
    var hash = hmac.finalize();
    return hash;
}

js output:

js_sign="MmUwZTI5NWNkMDg4YTYyYjRkNDJkZDgzMmQ0ZDdkYTk2NmZlMTcxNzZjNjE5OTRhZTBiMmU3NzFmOTEzY2QyODlkYWQ4YTA1MjRiMzY4OTU0Y2UyNDZhODc0ZjQxZTQ3YzkxNzQyNGM0M2ZiN2E3NjY5ZWJmNGRlZGY4MTRhZDY="

the correct sign is

correct_sign: Lg4pXNCIpitNQt2DLU19qWb+FxdsYZlK4LLncfkTzSidrYoFJLNolUziRqh09B5HyRdCTEP7enZp6/Te34FK1g==

how to get it in js?

Share Improve this question asked Jun 16, 2013 at 9:07 remykitsremykits 1,7754 gold badges18 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I suspect that you are running into trouble with types and encoding. According to the CryptoJS source, the iterative hashing style that you are using returns a WordArray once you call finalize().

With that, once you go to print the results, you are printing the contents of the WordArray.

The purpose for itterative hashing is typically if you have a large input, you can break it into chunks to work on one piece at a time. Try the below edit I made that removes this as it does not look like you need to iterate.

function hmac_512(message, secret) {
var newHMAC = CryptoJS.HmacSHA256(message, secret);
    return newHMAC;
}

The above will simply return the HMAC in string form which, once Base64 encoded, should match the result you see in Python.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论