ECDSA secp256r1
Getting Started
$ yarn add ecdsa-secp256r1
How to use
Node.js
Create key
const ECDSA = require('ecdsa-secp256r1')
const privateKey = ECDSA.generateKey()
privateKey.toJWK()
privateKey.asPublic().toJWK()
privateKey.toPEM()
privateKey.asPublic().toPEM()
privateKey.toCompressedPublicKey()
Sign
const message = { text: 'hello' }
privateKey.sign(JSON.stringify(message))
Verify
const key = 'A+GHVCISA53FbubUnII8bYYlzjv3pddRVm+eoEd6p4VU'
const publicKey = ECDSA.fromCompressedPublicKey(key)
const message = { text: 'hello' }
const signature = 'lY3Lf9xDtcsqom5IKu+ZyikxeYHlEuxnPfme4lMxp76NMkIm5BiLxVjbqBSo4itfT/LEuBCzMXl11cB0w/X8dA=='
publicKey.verify(JSON.stringify(message), signature)
Browsers
Support: https://caniuse.com/#feat=cryptography
const ECDSA = require('ecdsa-secp256r1/browser')
const privateKey = await ECDSA.generateKey()
or
<script src="/PATH/TO/browser.js"></script>
<script>
;(async function() {
const privateKey = await ECDSA.generateKey()
const message = { text: 'hello' }
const signature = await privateKey.sign(JSON.stringify(message))
})()
</script>
Thanks
Inspired from https://github.com/relocately/ec-key