Javascript 将cryto.generateKeyPair与jsonwebtoken一起使用
在节点10中,有一个新方法Javascript 将cryto.generateKeyPair与jsonwebtoken一起使用,javascript,node.js,jwt,Javascript,Node.js,Jwt,在节点10中,有一个新方法generateKeyPair,我使用的方法如下: const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", { modulusLength: 4096, publicKeyEncoding: { type: "spki", format: "pem" }, privateKeyEncoding: { type: "pkcs8", format:
generateKeyPair
,我使用的方法如下:
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 4096,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
cipher: "aes-256-cbc",
passphrase: "top secret"
}
});
我现在正尝试使用此私钥从jsonwebtoken
创建jwt:
function createJWT(id) {
return new Promise((resolve, reject) => {
jwt.sign(
{ id: id + "" },
privateKey,
{ algorithm: "RS256", expiresIn: "2h" },
(err, token) => {
if (err) reject(err);
resolve(token);
}
);
});
}
不幸的是,这似乎不起作用:
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Sign.sign (internal/crypto/sig.js:83:26)
at Object.sign (/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jwa/index.js:76:45)
at jwsSign (/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/sign-stream.js:32:24)
at SignStream.sign (/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/sign-stream.js:58:21)
at SignStream.<anonymous> (/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/sign-stream.js:46:12)
at Object.onceWrapper (events.js:273:13)
at DataStream.emit (events.js:182:13)
at DataStream.<anonymous> (/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/data-stream.js:32:12)
at process._tickCallback (internal/process/next_tick.js:61:11)
错误:错误:06065064:数字信封例程:EVP_decrypt最终_ex:错误解密
在Sign.Sign(内部/crypto/sig.js:83:26)
在Object.sign(/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jwa/index.js:76:45)
在jwsSign(/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/sign stream.js:32:24)
在SignStream.sign(/Users/francoisbillioud/Documents/Tests/SimpleQL/node_modules/jws/lib/sign stream.js:58:21)
在SignStream
我遗漏了什么?通过提供密码和密码短语,私钥将按照PKCS#5 v2.0基于密码的加密进行加密。jsonwebtoken
模块说明以下内容:
对于具有密码短语的私钥,对象{key,passphrase}
可以使用(基于加密文档),在这种情况下,请确保
传递算法选项
如果确实需要加密私钥,则需要保存私钥加密生成中使用的密码短语,并在sign()
函数中提供
let passphrase = 'top secret'
const { privateKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 4096,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
cipher: "aes-256-cbc",
passphrase
}
});
function createJWT(id) {
return new Promise((resolve, reject) => {
jwt.sign(
{
id: id + ""
},
{
key: privateKey,
passphrase
},
{
algorithm: "RS256",
expiresIn: "2h"
},
(err, token) => {
if (err) reject(err);
resolve(token);
}
);
});
}
哦好的,非常感谢!因此,如果我做对了,我可以从privateKeyEncoding
中删除passphrase
属性,或者提供{key,passphrase}
对象作为jsonwebtoken
的私钥?我马上试试。如果您的系统中不需要加密私钥本身,则必须删除generateKeyPairSync()
函数调用中的密码
和密码短语
字段。如果您觉得需要,那么是的,{key,passphrase}
对象作为jsonwebtoken
的私钥。实际上,我还需要删除cipher
字段。非常感谢。太好了,很高兴我能帮忙。