AWS v4 Signature with AWS SDK

node v4.9.1
version: 1.0.0
endpointsharetweet
Use AWS SDK to calculate an AWS v4 signature as described at http://docs.aws.amazon.com/general/latest/gr/signature-v4-test-suite.html#signature-v4-test-suite-task-3.
const AWS = require('aws-sdk'); const { createHmac } = AWS.util.crypto.lib; const hmacSha256 = (signingKey, stringToSign, encoding) => { return createHmac('sha256', signingKey) .update(stringToSign) .digest(encoding); }; const stringToSign = `AWS4-HMAC-SHA256 20150830T123600Z 20150830/us-east-1/service/aws4_request 816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0`; const targetSignature = 'b97d918cfa904a5beff61c982a1b6f458b799221646efd99d3219ec94cdf2500'; const secretKey = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'; const requestScope = stringToSign.split('\n')[2]; const [date, region, service, signatureType] = requestScope.split('/'); const round1 = hmacSha256(`AWS4${secretKey}`, date); const round2 = hmacSha256(round1, region); const round3 = hmacSha256(round2, service); const round4 = hmacSha256(round3, signatureType); const final = hmacSha256(round4, stringToSign, 'hex'); console.log(targetSignature == final.toString(), targetSignature, final.toString());
Loading…

no comments

    sign in to comment