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

Commit

Permalink
feat: nextTick instead of setImmediate, and fix sync in async (#136)
Browse files Browse the repository at this point in the history
* fix: avoid sync callback in async function

* chore: fix linting

* chore: remove non jenkins ci

* refactor: use nextTick over setImmediate

* refactor: async/nextTick for better browser support
  • Loading branch information
jacobheun authored and daviddias committed Jan 3, 2019
1 parent 857d2bd commit c54ea20
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 110 deletions.
32 changes: 0 additions & 32 deletions .travis.yml

This file was deleted.

29 changes: 0 additions & 29 deletions appveyor.yml

This file was deleted.

2 changes: 1 addition & 1 deletion benchmarks/ephemeral-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ curves.forEach((curve) => {

suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })
2 changes: 1 addition & 1 deletion benchmarks/key-stretcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async.waterfall([

suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })
})

function setup (cipher, hash, secret) {
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ suite.add('sign and verify', (d) => {

suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })
15 changes: 0 additions & 15 deletions circle.yml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
},
"devDependencies": {
"aegir": "^17.0.1",
"aegir": "^17.1.1",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-string": "^1.5.0",
Expand Down
8 changes: 4 additions & 4 deletions src/aes/index-browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const asm = require('asmcrypto.js')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')

exports.create = function (key, iv, callback) {
const done = (err, res) => setImmediate(() => callback(err, res))
const done = (err, res) => nextTick(() => callback(err, res))

if (key.length !== 16 && key.length !== 32) {
return done(new Error('Invalid key length'))
Expand All @@ -21,7 +21,7 @@ exports.create = function (key, iv, callback) {

const res = {
encrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))
const done = (err, res) => nextTick(() => cb(err, res))

let res
try {
Expand All @@ -36,7 +36,7 @@ exports.create = function (key, iv, callback) {
},

decrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))
const done = (err, res) => nextTick(() => cb(err, res))

let res
try {
Expand Down
4 changes: 2 additions & 2 deletions src/hmac/index-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const hashTypes = {
}

const sign = (key, data, cb) => {
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
nodeify(crypto.subtle.sign({ name: 'HMAC' }, key, data)
.then((raw) => Buffer.from(raw)), cb)
}

Expand All @@ -24,7 +24,7 @@ exports.create = function (hashType, secret, callback) {
secret,
{
name: 'HMAC',
hash: {name: hash}
hash: { name: hash }
},
false,
['sign']
Expand Down
5 changes: 4 additions & 1 deletion src/hmac/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const crypto = require('crypto')
const lengths = require('./lengths')
const nextTick = require('async/nextTick')

exports.create = function (hash, secret, callback) {
const res = {
Expand All @@ -10,7 +11,9 @@ exports.create = function (hash, secret, callback) {

hmac.update(data)

cb(null, hmac.digest())
nextTick(() => {
cb(null, hmac.digest())
})
},
length: lengths[hash]
}
Expand Down
6 changes: 3 additions & 3 deletions src/keys/ecdh.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const crypto = require('crypto')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')

const curves = {
'P-256': 'prime256v1',
Expand All @@ -16,7 +16,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
const ecdh = crypto.createECDH(curves[curve])
ecdh.generateKeys()

setImmediate(() => callback(null, {
nextTick(() => callback(null, {
key: ecdh.getPublicKey(),
genSharedKey (theirPub, forcePrivate, cb) {
if (typeof forcePrivate === 'function') {
Expand All @@ -35,7 +35,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
return cb(err)
}

setImmediate(() => cb(null, secret))
nextTick(() => cb(null, secret))
}
}))
}
10 changes: 5 additions & 5 deletions src/keys/ed25519.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const nacl = require('tweetnacl')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')

exports.publicKeyLength = nacl.sign.publicKeyLength
exports.privateKeyLength = nacl.sign.secretKeyLength

exports.generateKey = function (callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair()
Expand All @@ -20,7 +20,7 @@ exports.generateKey = function (callback) {

// seed should be a 32 byte uint8array
exports.generateKeyFromSeed = function (seed, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair.fromSeed(seed)
Expand All @@ -32,13 +32,13 @@ exports.generateKeyFromSeed = function (seed, callback) {
}

exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
callback(null, Buffer.from(nacl.sign.detached(msg, key)))
})
}

exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.detached.verify(msg, sig, key)
Expand Down
14 changes: 7 additions & 7 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports.generateKey = function (bits, callback) {
name: 'RSASSA-PKCS1-v1_5',
modulusLength: bits,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign', 'verify']
Expand All @@ -31,7 +31,7 @@ exports.unmarshalPrivateKey = function (key, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign']
Expand Down Expand Up @@ -59,13 +59,13 @@ exports.hashAndSign = function (key, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['sign']
).then((privateKey) => {
return webcrypto.subtle.sign(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
privateKey,
Uint8Array.from(msg)
)
Expand All @@ -78,13 +78,13 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['verify']
).then((publicKey) => {
return webcrypto.subtle.verify(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
publicKey,
sig,
msg
Expand All @@ -109,7 +109,7 @@ function derivePublicFromPrivate (jwKey) {
},
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['verify']
Expand Down
4 changes: 2 additions & 2 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
const multihashing = require('multihashing-async')
const protobuf = require('protons')
const bs58 = require('bs58')
const nextTick = require('async/nextTick')

const crypto = require('./rsa')
const pbm = protobuf(require('./keys.proto'))
const forge = require('node-forge')
const setImmediate = require('async/setImmediate')

class RsaPublicKey {
constructor (key) {
Expand Down Expand Up @@ -129,7 +129,7 @@ class RsaPrivateKey {

ensure(callback)

setImmediate(() => {
nextTick(() => {
let err = null
let pem = null
try {
Expand Down
13 changes: 7 additions & 6 deletions src/keys/rsa.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict'

const crypto = require('crypto')
const nextTick = require('async/nextTick')

let keypair
try {
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
throw new Error('Force keypair usage')
}

const ursa = require('ursa-optional') // throws if not compiled
keypair = ({bits}) => {
keypair = ({ bits }) => {
const key = ursa.generatePrivateKey(bits)
return {
private: key.toPrivatePem(),
Expand All @@ -22,14 +24,13 @@ try {

keypair = require('keypair')
}
const setImmediate = require('async/setImmediate')
const pemToJwk = require('pem-jwk').pem2jwk
const jwkToPem = require('pem-jwk').jwk2pem

exports.utils = require('./rsa-utils')

exports.generateKey = function (bits, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const key = keypair({ bits: bits })
Expand All @@ -47,7 +48,7 @@ exports.generateKey = function (bits, callback) {

// Takes a jwk key
exports.unmarshalPrivateKey = function (key, callback) {
setImmediate(() => {
nextTick(() => {
if (!key) {
return callback(new Error('Key is invalid'))
}
Expand All @@ -67,7 +68,7 @@ exports.getRandomValues = function (arr) {
}

exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const sign = crypto.createSign('RSA-SHA256')
Expand All @@ -83,7 +84,7 @@ exports.hashAndSign = function (key, msg, callback) {
}

exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const verify = crypto.createVerify('RSA-SHA256')
Expand Down

0 comments on commit c54ea20

Please sign in to comment.