Skip to content

Commit 9454548

Browse files
hillbradjasnell
authored andcommitted
doc: add example using algorithms not directly exposed
PR-URL: #6108 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
1 parent 7c9a691 commit 9454548

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/api/crypto.markdown

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,28 @@ console.log(sign.sign(private_key, 'hex'));
734734
// Prints the calculated signature
735735
```
736736

737+
A [`sign`][] instance can also be created by just passing in the digest
738+
algorithm name, in which case OpenSSL will infer the full signature algorithm
739+
from the type of the PEM-formatted private key, including algorithms that
740+
do not have directly exposed name constants, e.g. 'ecdsa-with-SHA256'.
741+
742+
Example: signing using ECDSA with SHA256
743+
744+
```js
745+
const crypto = require('crypto');
746+
const sign = crypto.createSign('sha256');
747+
748+
sign.update('some data to sign');
749+
750+
const private_key = '-----BEGIN EC PRIVATE KEY-----\n' +
751+
'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' +
752+
'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' +
753+
'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' +
754+
'-----END EC PRIVATE KEY-----\n';
755+
756+
console.log(sign.sign(private_key).toString('hex'));
757+
```
758+
737759
### sign.sign(private_key[, output_format])
738760

739761
Calculates the signature on all the data passed through using either

0 commit comments

Comments
 (0)