Monday, 19 September 2022

javascript HMAC generator code convert to python return different results

I got the following Javascript code and I need to convert it to Python(I'm not an expert in hashing so sorry for my knowledge on this subject)

function generateAuthHeader(dataToSign) {
    let apiSecretHash = new Buffer("Rbju7azu87qCTvZRWbtGqg==", 'base64');
    let apiSecret = apiSecretHash.toString('ascii');
    var hash = CryptoJS.HmacSHA256(dataToSign, apiSecret);
    return hash.toString(CryptoJS.enc.Base64);
}

when I ran generateAuthHeader("abc") it returned +jgBeooUuFbhMirhh1KmQLQ8bV4EXjRorK3bR/oW37Q=

So I tried writing the following Python code:

def generate_auth_header(data_to_sign):
    api_secret_hash = bytearray(base64.b64decode("Rbju7azu87qCTvZRWbtGqg=="))
    hash = hmac.new(api_secret_hash, data_to_sign.encode(), digestmod=hashlib.sha256).digest()
    return base64.b64encode(hash).decode()

But when I ran generate_auth_header("abc") it returned a different result aOGo1XCa5LgT1CIR8C1a10UARvw2sqyzWWemCJBJ1ww=

Can someone tell me what is wrong with my Python code and what I need to change?

The base64 is the string I generated myself for this post

UPDATE: this is the document I'm working with

//Converting the Rbju7azu87qCTvZRWbtGqg== (key) into byte array 
//Converting the data_to_sign into byte array 
//Generate the hmac signature


from javascript HMAC generator code convert to python return different results

No comments:

Post a Comment