无法使用nodejs加密签名文件

时间:2016-09-26 07:21:00

标签: node.js header signature private-key elliptic-curve

我已经使用nodejs加密创建了一个私钥,并希望使用此密钥签名文件。 我的代码如下:

var ecdh = crypto.createECDH('brainpoolP512t1');
        ecdh.generateKeys();
        var key = ecdh.getPrivateKey('buffer');

        var data= fs.readFileSync(req.file.path);
        var sign = crypto.createSign('sha512');
        sign.update(data);
        var signature = sign.sign(key, 'hex');

但我收到错误:

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
    at Error (native)
    at Sign.sign (crypto.js:283:26)
    at /....js:32:27
    at Immediate.<anonymous> (/.../node_modules/multer/lib/make-middleware.js:52:37)
    at runCallback (timers.js:578:20)
    at tryOnImmediate (timers.js:554:5)
    at processImmediate [as _immediateCallback] (timers.js:533:5)

我知道它与密钥格式有关,但我不知道如何解决这个问题。有人可以帮忙吗?

更新: 我编辑了privateKey以适合pem格式:

var KEY_START = '-----BEGIN EC PRIVATE KEY-----\n';
var KEY_END = '\n-----END EC PRIVATE KEY-----';

const ecdh = crypto.createECDH('brainpoolP512t1');
            ecdh.generateKeys();
            var key =KEY_START + ecdh.getPrivateKey('base64') + KEY_END;        
            var data= fs.readFileSync(req.file.path);
            const sign = crypto.createSign('sha512');
            sign.update(data);
            var signature = sign.sign(key, 'hex');

现在我犯了一个不同的错误:

Error: error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long
    at Error (native)
    at Sign.sign (crypto.js:283:26)
    at /...js:37:27
    at Immediate.<anonymous> (/.../node_modules/multer/lib/make-middleware.js:52:37)
    at runCallback (timers.js:578:20)
    at tryOnImmediate (timers.js:554:5)
    at processImmediate [as _immediateCallback] (timers.js:533:5)

1 个答案:

答案 0 :(得分:4)

您对数据签名的密钥需要是有效的PEM编码私钥。 DH getPrivateKey()函数不返回此格式的密钥,它返回裸私钥数据。

您的选择包括:

  • 通过OpenSSL密钥生成器实用程序或类似的
  • 生成私钥
  • 使用第三方节点模块正确编码私钥,如RFC 5915中所述。使用asn1.jsbn.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-encoded private key
    var pemKey = ECPrivateKey.encode({
      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);