refactor: make rsa key generation sync

This commit is contained in:
dignifiedquire 2016-05-23 19:13:31 +02:00
parent fba4d3cc0f
commit 1270f3e37e
7 changed files with 37 additions and 1570 deletions

View File

@ -18,11 +18,10 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
## API
### `generateKeyPair(type, bits, cb)`
### `generateKeyPair(type, bits)`
- `type: String`, only `'RSA'` is currently supported
- `bits: Number`
- `cb: Function`, with the signature `function (err, privateKey)`
Generates a keypair of the given type and bitsize.

View File

@ -12,13 +12,13 @@ exports.keyStretcher = require('./key-stretcher')
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
// Generates a keypair of the given type and bitsize
exports.generateKeyPair = (type, bits, cb) => {
exports.generateKeyPair = (type, bits) => {
let key = keys[type.toLowerCase()]
if (!key) {
throw new Error('invalid or unsupported key type')
}
key.generateKeyPair(bits, cb)
return key.generateKeyPair(bits)
}
// Converts a protobuf serialized public key into its

View File

@ -55,7 +55,11 @@ class RsaPublicKey {
class RsaPrivateKey {
constructor (privKey, pubKey) {
this._privateKey = privKey
this._publicKey = pubKey
if (pubKey) {
this._publicKey = pubKey
} else {
this._publicKey = forge.pki.setRsaPublicKey(privKey.n, privKey.e)
}
}
genSecret () {
@ -123,15 +127,9 @@ function unmarshalRsaPublicKey (bytes) {
return new RsaPublicKey(key)
}
function generateKeyPair (bits, cb) {
rsa.generateKeyPair({
bits,
workerScript: utils.workerScript
}, (err, p) => {
if (err) return cb(err)
cb(null, new RsaPrivateKey(p.privateKey, p.publicKey))
})
function generateKeyPair (bits) {
const p = rsa.generateKeyPair({bits})
return new RsaPrivateKey(p.privateKey, p.publicKey)
}
module.exports = {

View File

@ -1,40 +1,8 @@
'use strict'
const multihashing = require('multihashing')
const path = require('path')
const fs = require('fs')
const URL = global.window && (window.URL || window.webkitURL)
// Hashes a key
exports.keyHash = (bytes) => {
return multihashing(bytes, 'sha2-256')
}
const toBlob = (content) => {
try {
let blob
try {
// BlobBuilder = Deprecated, but widely implemented
const BlobBuilder = global.window &&
(window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder)
blob = new BlobBuilder()
blob.append(content)
blob = blob.getBlob()
} catch (e) {
// The proposed API
blob = new window.Blob([content])
}
return URL.createObjectURL(blob)
} catch (e) {
return 'data:application/javascript,' + encodeURIComponent(content)
}
}
const rawScript = fs.readFileSync(path.join(__dirname, '../vendor/prime.worker.js'))
exports.workerScript = toBlob(rawScript.toString())

View File

@ -8,12 +8,8 @@ const fixtures = require('./fixtures/go-key-rsa')
describe('libp2p-crypto', () => {
let key
before((done) => {
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
if (err) return done(err)
key = _key
done()
})
before(() => {
key = crypto.generateKeyPair('RSA', 2048)
})
it('marshalPublicKey and unmarshalPublicKey', () => {

View File

@ -8,12 +8,8 @@ const rsa = crypto.keys.rsa
describe('RSA', () => {
let key
before((done) => {
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
if (err) return done(err)
key = _key
done()
})
before(() => {
key = crypto.generateKeyPair('RSA', 2048)
})
it('generates a valid key', () => {
@ -80,36 +76,32 @@ describe('RSA', () => {
)
})
it('not equals other key', (done) => {
crypto.generateKeyPair('RSA', 2048, (err, key2) => {
if (err) return done(err)
it('not equals other key', () => {
const key2 = crypto.generateKeyPair('RSA', 2048)
expect(
key.equals(key2)
).to.be.eql(
false
)
expect(
key.equals(key2)
).to.be.eql(
false
)
expect(
key2.equals(key)
).to.be.eql(
false
)
expect(
key2.equals(key)
).to.be.eql(
false
)
expect(
key.public.equals(key2.public)
).to.be.eql(
false
)
expect(
key.public.equals(key2.public)
).to.be.eql(
false
)
expect(
key2.public.equals(key.public)
).to.be.eql(
false
)
done()
})
expect(
key2.public.equals(key.public)
).to.be.eql(
false
)
})
})

1486
vendor/prime.worker.js vendored

File diff suppressed because it is too large Load Diff