【发布时间】:2018-06-15 11:06:57
【问题描述】:
Unable to sign a file with nodejs crypto
我正在尝试使用方法 verify.verify() 和 ECDH 公钥来验证在此线程中创建的签名文档。因此,我想,我必须将原始公钥格式化为有效的 PEM。
我将如何使用 ans1.js 和 bn.js 模块来做到这一点?
【问题讨论】:
标签: node.js sign cryptojs verify diffie-hellman
Unable to sign a file with nodejs crypto
我正在尝试使用方法 verify.verify() 和 ECDH 公钥来验证在此线程中创建的签名文档。因此,我想,我必须将原始公钥格式化为有效的 PEM。
我将如何使用 ans1.js 和 bn.js 模块来做到这一点?
【问题讨论】:
标签: node.js sign cryptojs verify diffie-hellman
web-push 库就是这样做的:
const asn1 = require('asn1.js');
const ECPrivateKeyASN = asn1.define('ECPrivateKey', function() {
this.seq().obj(
this.key('version').int(),
this.key('privateKey').octstr(),
this.key('parameters').explicit(0).objid()
.optional(),
this.key('publicKey').explicit(1).bitstr()
.optional()
);
});
function toPEM(key) {
return ECPrivateKeyASN.encode({
version: 1,
privateKey: key,
parameters: [1, 2, 840, 10045, 3, 1, 7] // prime256v1
}, 'pem', {
label: 'EC PRIVATE KEY'
});
}
【讨论】: