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

@ -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())