
用于签署数据的密钥必须是有效的PEM编码的私钥。DH
getPrivateKey()函数不以这种格式返回密钥,而是返回裸机专用密钥数据。
您的选择包括:
asn1.js和
bn.js模块的完整示例:
var crypto = require('crypto'); var asn1 = require('asn1.js'); var BN = require('bn.js'); function toOIDArray(oid) { return oid.split('.').map(function(s) { return parseInt(s, 10) }); } // Define ECPrivateKey from RFC 5915 var ECPrivateKey = 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() ); }); // Generate the DH keys var ecdh = crypto.createECDH('brainpoolP512t1'); ecdh.generateKeys(); // Generate the PEM-enpred private key var pemKey = ECPrivateKey.enpre({ version: new BN(1), privateKey: ecdh.getPrivateKey(), // OID for brainpoolP512t1 parameters: toOIDArray('1.3.36.3.3.2.8.1.1.14') }, 'pem', { label: 'EC PRIVATE KEY' }); // Sign data var sign = crypto.createSign('sha512'); sign.update('hello world'); var signature = sign.sign(pemKey, 'hex'); console.log('signature', signature);