I've created a private Key with nodejs crypto and want to sign a file with this key.
My code is following:
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');
But I get the error:
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)
I know it has something to do with the key format, but I don't know how to fix this. Can anyone help?
UPDATE:
I edited the privateKey to fit the pem format:
var KEY_START = '-----BEGIN EC PRIVATE KEY-----
';
var KEY_END = '
-----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');
And now I geht a different error:
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…