Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

feat: replace keypair and ursa-optional with web-crypto for node #218

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node: [14, 16]
node: [16]
fail-fast: true
steps:
- uses: actions/checkout@v2
Expand Down
87 changes: 87 additions & 0 deletions benchmarks/rsa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable no-console */
'use strict'

const Benchmark = require('benchmark')
const { subtle } = require('crypto').webcrypto
const ursa = require('ursa-optional')
const crypto = require('crypto')
const keypair = require('keypair')

const suite = new Benchmark.Suite('rsa implementations')
const BITS = 512

suite.add('ursa-optional', async (d) => {
const message = Buffer.from('hello world ' + Math.random())
const keyPair = ursa.generatePrivateKey(BITS)

const signature = crypto.createSign('RSA-SHA256')
.update(message)
.sign(keyPair.toPrivatePem())

const isSigned = crypto.createVerify('RSA-SHA256')
.update(message)
.verify(keyPair.toPublicPem(), signature)

if (!isSigned) {
throw new Error('could not verify ursa-optional signature')
}

d.resolve()
}, { defer: true })

suite.add('keypair', async (d) => {
const message = Buffer.from('hello world ' + Math.random())
const keyPair = keypair(BITS)

const signature = crypto.createSign('RSA-SHA256')
.update(message)
.sign(keyPair.private)

const isSigned = crypto.createVerify('RSA-SHA256')
.update(message)
.verify(keyPair.public, signature)

if (!isSigned) {
throw new Error('could not verify keypair signature')
}

d.resolve()
}, { defer: true })

suite.add('node.js web-crypto', async (d) => {
const message = Buffer.from('hello world ' + Math.random())

const keyPair = await subtle.generateKey({
name: 'RSASSA-PKCS1-v1_5',
modulusLength: BITS,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-256' }
},
true,
['sign', 'verify']
)

const signature = await subtle.sign('RSASSA-PKCS1-v1_5', keyPair.privateKey, message)
const isSigned = await subtle.verify('RSASSA-PKCS1-v1_5', keyPair.publicKey, signature, message)

if (!isSigned) {
throw new Error('could not verify node.js signature')
}

d.resolve()
}, { defer: true })

async function main () {
suite
.on('cycle', (event) => console.log(String(event.target)))
.on('complete', function () {
console.log('fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
}

main()
.catch(err => {
console.error(err)
process.exit(1)
})
14 changes: 14 additions & 0 deletions benchmarks/rsa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "libp2p-crypto-rsa-benchmarks",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ."
},
"license": "MIT",
"dependencies": {
"iso-random-stream": "^2.0.0",
"keypair": "^1.0.4",
"ursa-optional": "^0.10.2"
}
}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"./src/ciphers/aes-gcm.js": "./src/ciphers/aes-gcm.browser.js",
"./src/hmac/index.js": "./src/hmac/index-browser.js",
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js",
"./src/keys/rsa.js": "./src/keys/rsa-browser.js"
"./src/webcrypto.js": "./src/webcrypto-browser.js"
},
"files": [
"src",
Expand Down Expand Up @@ -44,13 +44,11 @@
"@noble/secp256k1": "^1.3.0",
"err-code": "^3.0.1",
"iso-random-stream": "^2.0.0",
"keypair": "^1.0.4",
"multiformats": "^9.4.5",
"node-forge": "^0.10.0",
"pem-jwk": "^2.0.0",
"protobufjs": "^6.11.2",
"uint8arrays": "^3.0.0",
"ursa-optional": "^0.10.1"
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@types/mocha": "^9.0.0",
Expand Down
153 changes: 0 additions & 153 deletions src/keys/rsa-browser.js

This file was deleted.

Loading