【发布时间】:2010-06-01 20:44:20
【问题描述】:
所以我试图在我的 javascript 中调用此函数,但它给了我“Microsoft JScript 运行时错误对象不支持此属性或方法”的错误,我不知道为什么。它在尝试调用 hmacObj.getHMAC 时发生。这是来自 jsSHA 网站:http://jssha.sourceforge.net/ 使用 hmac-sha1 算法加密。谢谢!
hmacObj = new jsSHA(signature_base_string,"HEX");
signature = hmacObj.getHMAC(signature_key,"HEX","SHA-1","HEX");
在此之上,我从 sha.js 复制了代码
sn-p:
function jsSHA(srcString, inputFormat) {
/*
* Configurable variables. Defaults typically work
*/
jsSHA.charSize = 8; // Number of Bits Per character (8 for ASCII, 16 for Unicode)
jsSHA.b64pad = ""; // base-64 pad character. "=" for strict RFC compliance
jsSHA.hexCase = 0; // hex output format. 0 - lowercase; 1 - uppercase
var sha1 = null;
var sha224 = null;
它正在调用的函数(在 jsSHA 函数内部)
this.getHMAC = function (key, inputFormat, variant, outputFormat) {
var formatFunc = null;
var keyToUse = null;
var blockByteSize = null;
var blockBitSize = null;
var keyWithIPad = [];
var keyWithOPad = [];
var lastArrayIndex = null;
var retVal = null;
var keyBinLen = null;
var hashBitSize = null;
// Validate the output format selection
switch (outputFormat) {
case "HEX":
formatFunc = binb2hex;
break;
case "B64":
formatFunc = binb2b64;
break;
default:
return "FORMAT NOT RECOGNIZED";
}
// Validate the hash variant selection and set needed variables
switch (variant) {
case "SHA-1":
blockByteSize = 64;
hashBitSize = 160;
break;
case "SHA-224":
blockByteSize = 64;
hashBitSize = 224;
break;
case "SHA-256":
blockByteSize = 64;
hashBitSize = 256;
break;
case "SHA-384":
blockByteSize = 128;
hashBitSize = 384;
break;
case "SHA-512":
blockByteSize = 128;
hashBitSize = 512;
break;
default:
return "HASH NOT RECOGNIZED";
}
// Validate input format selection
if ("HEX" === inputFormat) {
// Nibbles must come in pairs
if (0 !== (key.length % 2)) {
return "KEY MUST BE IN BYTE INCREMENTS";
}
keyToUse = hex2binb(key);
keyBinLen = key.length * 4;
} else if ("ASCII" === inputFormat) {
keyToUse = str2binb(key);
keyBinLen = key.length * jsSHA.charSize;
} else {
return "UNKNOWN KEY INPUT TYPE";
}
// These are used multiple times, calculate and store them
blockBitSize = blockByteSize * 8;
lastArrayIndex = (blockByteSize / 4) - 1;
// Figure out what to do with the key based on its size relative to
// the hash's block size
if (blockByteSize < (keyBinLen / 8)) {
if ("SHA-1" === variant) {
keyToUse = coreSHA1(keyToUse, keyBinLen);
} else {
keyToUse = coreSHA2(keyToUse, keyBinLen, variant);
}
// For all variants, the block size is bigger than the output size
// so there will never be a useful byte at the end of the string
keyToUse[lastArrayIndex] &= 0xFFFFFF00;
} else if (blockByteSize > (keyBinLen / 8)) {
// If the blockByteSize is greater than the key length, there will
// always be at LEAST one "useless" byte at the end of the string
keyToUse[lastArrayIndex] &= 0xFFFFFF00;
}
// Create ipad and opad
for (var i = 0; i <= lastArrayIndex; i++) {
keyWithIPad[i] = keyToUse[i] ^ 0x36363636;
keyWithOPad[i] = keyToUse[i] ^ 0x5C5C5C5C;
}
// Calculate the HMAC
if ("SHA-1" === variant) {
retVal = coreSHA1(keyWithIPad.concat(strToHash), blockBitSize + strBinLen);
retVal = coreSHA1(keyWithOPad.concat(retVal), blockBitSize + hashBitSize);
} else {
retVal = coreSHA2(keyWithIPad.concat(strToHash), blockBitSize + strBinLen, variant);
retVal = coreSHA2(keyWithOPad.concat(retVal), blockBitSize + hashBitSize, variant);
}
return (formatFunc(retVal));
};
【问题讨论】:
-
您确定在此之前没有抛出错误吗?
-
我已经记录下来给我看。并且整个函数定义都在这个错误之上。 2010-06-01 14:36:22 T08A0 错误 - scriptedtest.cpp(102) -Script: scripts/BuildOAuthTwitterRequest.js at .失败的。错误:Microsoft JScript 运行时错误对象不支持此属性或方法 EXCEPINFO.scode = 0x800a01b6 (FormatMessage() 错误 317 (0x13d) 翻译错误 2148139446 (0x800a01b6)) -------------- ------------------ 签名 = hmacObj.getHMAC(signature_key,"HEX","SHA-1","HEX"); .\scripts\BuildOAuthTwitterRequest.js 的第 1207 行
-
您是否使用 firebug 或 IE 开发工具进入 getHMAC?
-
那么脚本是通过 C++ 运行的,所以它不在 IE 或 firefox 中查看,它在发送通过日志文件输出的 http 请求时工作正常。可能是 JScript 引擎,如果我弄清楚了,我会告诉你。
标签: javascript