在我的api提供程序中,我有一个代码可以生成hmac密钥。
<html>
<head>
</head>
<body>
<p id="demo"></p>
<script>var BuckarooHmac = (function () {
var self = {};
function getEncodedContent(content) {
if (content) {
var md5 = CryptoJS.MD5(content);
var base64 = CryptoJS.enc.Base64.stringify(md5);
return base64;
}
return content;
}
function getHash(websiteKey, secretKey, httpMethod, nonce, timeStamp, requestUri, content) {
var encodedContent = getEncodedContent(content);
var rawData = websiteKey + httpMethod + requestUri + timeStamp + nonce + encodedContent;
var hash = CryptoJS.HmacSHA256(rawData, secretKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
return hashInBase64;
}
function getTimeStamp() {
return Math.floor((new Date).getTime() / 1000);
}
function getNonce() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 16; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
self.GetAuthHeader = function (requestUri, websiteKey, secretKey, content, httpMethod) {
var nonce = getNonce();
var timeStamp = getTimeStamp();
content = content ? content : "";
var url = encodeURIComponent(requestUri).toLowerCase();
return "hmac " + websiteKey + ":" + getHash(websiteKey, secretKey, httpMethod, nonce, timeStamp, url, content) + ":" + nonce + ":" + timeStamp;
}
return self;
document.getElementById("demo").innerHTML = self.GetAuthHeader();
}());
</script>
</body>
</html>
我不习惯使用javascript。我试图弄清楚如何在屏幕上打印生成的密钥。我尝试了这个:
document.getElementById("demo").innerHTML = self.GetAuthHeader();
我知道我一定做错了。我现在只需要朝正确的方向推进。有人可以帮助我吗?
答案 0 :(得分:1)
您要尝试在函数的return语句后 执行操作。因为该函数已返回,所以永远不会到达该代码。
相反,请先执行以下操作:
document.getElementById("demo").innerHTML = self.GetAuthHeader();
return self;
或者更好的是,如果此代码是由供应商提供的,那么您可能不应该对其进行编辑。更新将删除您的编辑,并且供应商支持将受到损害。相反,请完全在代码外执行操作:
var BuckarooHmac = (function () {
// vendor code
}());
document.getElementById("demo").innerHTML = BuckarooHmac.GetAuthHeader();