You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I didn't know where to ask this. But I needed to ask.
The problem is after generating public private keys with ecdh, I am trying to use sign.sign and it doesn't seem to work.
At first I just wrapped the alice.getPrivateKey('base64') inside a -----BEGIN EC PRIVATE KEY----- and -----END EC PRIVATE KEY----- but that resulted in asn1 too long
So I split the private key at 64 characters, and that resulted in Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
I also tried spliting the `alice.generateKeys().toString('base64') and wrapping it in begin end block. But that too results in the same error.
Here are the codes:
try1.js
const crypto = require('crypto');
// Generate Alice's keys...
const alice = crypto.createECDH('secp521r1');
const aliceKeys = alice.generateKeys();
let pKey = `-----BEGIN EC PRIVATE KEY-----\n${aliceKeys.toString('base64').match(/.{1,64}/g).join('\n')}\n-----END EC PRIVATE KEY-----`;
console.log(pKey)
const sign = crypto.createSign('ecdsa-with-SHA1');
sign.update('some data to sign');
console.log(sign.sign(pkey, 'hex'))
try2.js
const alice = crypto.createECDH('secp521r1');
alice.generateKeys();
const alicePKey = alice.getPrivateKey('base64')
let privateKey = `-----BEGIN EC PRIVATE KEY-----\n${alicePKey.match(/.{1,64}/g).join('\n')}\n-----END EC PRIVATE KEY-----`;
console.log(privateKey)
const sign = crypto.createSign('ecdsa-with-SHA1');
sign.update('some data to sign');
console.log(sign.sign(privateKey, 'hex'))
The error being:
crypto.js:286
var ret = this._handle.sign(toBuf(key), null, passphrase);
^
Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
at Sign.sign (crypto.js:286:26)
at Object.<anonymous> (/home/argentum/dev/dfh.js:15:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
Node.js currently has no direct support for constructing that container but there's probably a library for it on npm (asn1.js?) and worst case, you can construct it yourself, the format is not hugely complex.
I'll close this out, see #15116 for the feature request. Please post follow-up questions to the help repo.
Why doesn't the Sign#sign method allow the use of a Buffer to pass along the private key to use? It seems like quite a waste to first add a wrapper Sign#sign needs to unwrap anyway.
@skerit Just raw bytes doesn't tell you what kind of key it is or what params it has. Look up how many openssl apis start with 'EVP_PKEY_' and you'll get a feel for the phase space. :-)
I didn't know where to ask this. But I needed to ask.
The problem is after generating public private keys with ecdh, I am trying to use
sign.sign
and it doesn't seem to work.At first I just wrapped the
alice.getPrivateKey('base64')
inside a-----BEGIN EC PRIVATE KEY-----
and-----END EC PRIVATE KEY-----
but that resulted inasn1 too long
So I split the private key at 64 characters, and that resulted in
Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
I also tried spliting the `alice.generateKeys().toString('base64') and wrapping it in begin end block. But that too results in the same error.
Here are the codes:
try1.js
try2.js
The error being:
But if I generate the pem file like:
which is from where I copied the begin end blocks. And I read the file in ascii and pass that to the
sign
method. It works like butter.What am I doing wrong
The text was updated successfully, but these errors were encountered: