CryptoJS:key.clamp不是函数

Mid*_*ori 5 javascript jwt cryptojs

TypeError: key.clamp is not a function
  at Object.init (path/node_modules/crypto-js/hmac.js:58:18)
Run Code Online (Sandbox Code Playgroud)

当我尝试使用下面的相关代码在Javascript中创建JWT时,会发生上述错误。

const CryptoJS = require('crypto-js');
var hash = CryptoJS.HmacSHA256(token.join("."), secret);
Run Code Online (Sandbox Code Playgroud)

crypto-js / hmac.js:58:18有key.clamp();,我不确定哪种方法最好。我尝试过,HmacSHA512但它返回相同的错误。

我正在跑步npm 6.1.0 node v6.10.3 crypto-js ^3.1.9-1

Ade*_*lin 8

他们的样本来看,secret(或者key他们这样称呼它)应该是一个string.

因此,CryptoJS像这样使用应该可以正常工作:

const token = "a,b"; // fake token
const secret = CryptoJS.enc.Utf8.parse("mySecret"); //encode mySecret into UTF-8 as suggested in the comments
const CryptoJS = require('crypto-js');
var hash = CryptoJS.HmacSHA256(token.split(","), secret);
console.log(hash);
Run Code Online (Sandbox Code Playgroud)

  • `CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(plainTextPassword));` 但我使用它进行 base64 编码。也许我搞乱了你的代码哈哈 (4认同)