I have following bash script:
#!/bin/bash
file="$1"
bucket="mybucket"
resource="/${bucket}/${file}"
contentType="image/png"
dateValue="Thu, 10 Mar 2016 04:13:24 +0545"
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
vs3Key="AKXXXXXXXXXXXXXXXXXXX"
s3Secret="/Wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
echo $signature
I want to write this same code in nodejs. This is what I have written so far, but the signature is not valid:
'use strict';
var secret= '/Wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var file = process.argv[2];
var crypto = require('crypto');
var dateValue = 'Thu, 10 Mar 2016 04:13:24 +0545';
var bucket = "mybucket";
var resource = "/" + bucket + "/" + file;
var contentType = "image/png";
var stringToSign = "PUT\n\n"+contentType+"\n"+dateValue+"\n"+resource;
var s3Key = "AKxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hmac = crypto.createHmac("sha1", secret).update(stringToSign).digest('hex');
console.log(new Buffer(hmac).toString('base64'));
Your bash code is calling openssl
with the -binary
argument, so you should be doing the same in the JS code.
According to the documentation :
hash.digest([encoding])
Calculates the digest of all of the data passed to be hashed (using the
hash.update()
method). Theencoding
can be'hex'
,'binary'
or'base64'
. Ifencoding
is provided a string will be returned; otherwise aBuffer
is returned.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.