How can I create signature for AWS API in javascript using HmacSHA256?

前端 未结 1 1579
挽巷
挽巷 2021-01-28 03:08

I\'m trying to make a request to AWS API using Javascript, and starting with their examples provided here

I\'m using hmac-sha256.js library for that too.

Here is

相关标签:
1条回答
  • 2021-01-28 03:21

    Finally it works now after I applied this answer

    I was converting sig.toString() to base64 using a simple function, but using enc-base64.js library is converting wordArray to base64, I don't know what is the different really and I hope someone explain to me that :)

    Here is a function I used:

    function base64Encode(str) {
    var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var out = "", i = 0, len = str.length, c1, c2, c3;
    while (i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if (i == len) {
            out += CHARS.charAt(c1 >> 2);
            out += CHARS.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i == len) {
            out += CHARS.charAt(c1 >> 2);
            out += CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += CHARS.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += CHARS.charAt(c1 >> 2);
        out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
        out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
        out += CHARS.charAt(c3 & 0x3F);
    }
    return out;
    }
    

    Thanks.

    0 讨论(0)
提交回复
热议问题