-
Notifications
You must be signed in to change notification settings - Fork 51
feat(rsa): always use the fastest method available for rsa keygeneration #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "libp2p-crypto", | ||
"version": "0.6.1", | ||
"description": "Crypto primitives for libp2p", | ||
"main": "lib/index.js", | ||
"main": "src/index.js", | ||
"jsnext:main": "src/index.js", | ||
"scripts": { | ||
"lint": "aegir-lint", | ||
|
@@ -55,5 +55,8 @@ | |
"Friedel Ziegelmayer <[email protected]>", | ||
"Richard Littauer <[email protected]>", | ||
"greenkeeperio-bot <[email protected]>" | ||
] | ||
} | ||
], | ||
"optionalDependencies": { | ||
"ursa": "^0.9.4" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,9 +127,39 @@ function unmarshalRsaPublicKey (bytes) { | |
return new RsaPublicKey(key) | ||
} | ||
|
||
function generateKeyPair (bits) { | ||
const p = rsa.generateKeyPair({bits}) | ||
return new RsaPrivateKey(p.privateKey, p.publicKey) | ||
function fastKeys (bits, cb) { | ||
if (typeof window === 'undefined') { | ||
let ursa | ||
try { | ||
ursa = require('ursa') | ||
} catch (err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should at least show a warning if something goes wrong here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? This is simply the check if the optional dependency is available to be used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, did not know is what an optional dependency and my spidey senses goes off when I see a empty catch in a try/catch block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As they should :) |
||
} | ||
|
||
if (ursa && ursa.generatePrivateKey) { | ||
const key = ursa.generatePrivateKey(bits, 65537) | ||
cb(null, { | ||
privateKey: pki.privateKeyFromPem(key.toPrivatePem().toString()), | ||
publicKey: pki.publicKeyFromPem(key.toPublicPem().toString()) | ||
}) | ||
return | ||
} | ||
} | ||
|
||
rsa.generateKeyPair({ | ||
bits, | ||
workers: -1, | ||
workerScript: utils.workerScript | ||
}, cb) | ||
} | ||
|
||
function generateKeyPair (bits, cb) { | ||
fastKeys(bits, (err, p) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, new RsaPrivateKey(p.privateKey, p.publicKey)) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const multihashing = require('multihashing') | ||
|
||
// 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 window.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()) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.