该应用程序遵循node-jose 2.0.0
导入.pem
密钥的说明。这是文档:
To import and existing Key from a PEM or DER:
// input is either a:
// * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// form is either a:
// * "json" for a JSON stringified JWK
// * "private" for a DER encoded 'raw' private key
// * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key
// * "public" for a DER encoded SPKI public key (alternate to 'spki')
// * "spki" for a DER encoded SPKI public key
// * "pkix" for a DER encoded PKIX X.509 certificate
// * "x509" for a DER encoded PKIX X.509 certificate
// * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX //<<=="pem"
keystore.add(input, form).
then(function(result) {
// {result} is a jose.JWK.Key
});
密钥已经使用.pem
表单生成,其内容存储在 nodejs 配置文件中,如下所示process.env.josePrivateKey
:
-----BEGIN PRIVATE KEY-----
NC4CAQAwBQYcK2VwBCIEIIWUb0/MoKaBxQkmmPlHIGyPfDQb/U3D6jQ+gMUGtvpa
-----END PRIVATE KEY-----
这是将密钥添加pem
到密钥库的代码:
const jose = require('node-jose');
let keystore = jose.JWK.createKeyStore();
let privkey = await keystore.add(process.env.josePrivateKey, "pem"); //<<==this code throws error
但是有一个错误:
(node:11572) UnhandledPromiseRejectionWarning: Error: no importer for key
at JWKStore.value (C:\d\code\js\xyz\node_modules\node-jose\lib\jwk\keystore.js:305:21)
at initKeystore (C:\d\code\js\xyz\startup\accessstorageinfo.js:9:34) //<<==code as above
at Object.<anonymous> (C:\d\code\js\xyz\startup\accessstorageinfo.js:14:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\d\code\js\xyz\server.js:13:1)
密钥导入这里缺少什么?