我有以下bash脚本:
#!/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
我想在nodejs中编写相同的代码。这是我到目前为止所写的,但签名无效:
'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'));
答案 0 :(得分:2)
你的bash代码使用openssl
参数调用-binary
,所以你应该在JS代码中做同样的事情。
hash.digest([encoding])
计算传递给要散列的所有数据的摘要(使用
hash.update()
方法)。encoding
可以是'hex'
,'binary'
或'base64'
。如果提供encoding
,则返回一个字符串;否则返回Buffer
。