Permalink
Browse files

refactor(crypto): fall back to node's scrypt implementation

  • Loading branch information...
philbooth committed Feb 1, 2019
1 parent 415a998 commit 932f2dd755b0d6c1df2aba23cb2fec8c05e91b49
Showing with 60 additions and 29 deletions.
  1. +27 −1 lib/db/util.js
  2. +30 −27 npm-shrinkwrap.json
  3. +3 −1 package.json
@@ -7,9 +7,35 @@
const crypto = require('crypto')
const P = require('../promise')
const randomBytes = P.promisify(require('crypto').randomBytes)
const scryptHash = P.promisify(require('scrypt-hash'))
const base32 = require('./random')

// Magic numbers from the node crypto docs:
// https://nodejs.org/api/crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
const DEFAULT_N = 16384
const DEFAULT_R = 8
const MAXMEM_MULTIPLIER = 256
const DEFAULT_MAXMEM = MAXMEM_MULTIPLIER * DEFAULT_N * DEFAULT_R

let scryptHash
try {
scryptHash = P.promisify(require('scrypt-hash'))
} catch (err) {
if (! crypto.scrypt) {
throw new Error('Missing scrypt implementation')
}

const scryptP = P.promisify(crypto.scrypt)
scryptHash = (input, salt, N, r, p, len) => {
let maxmem = DEFAULT_MAXMEM
if (N > DEFAULT_N || r > DEFAULT_R) {
// Conservatively prevent `memory limit exceeded` errors. See the docs for more info:
// https://nodejs.org/api/crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
maxmem = MAXMEM_MULTIPLIER * (N || DEFAULT_N) * (r || DEFAULT_R)
}
return scryptP(input, salt, len, { N, r, p, maxmem })
}
}

const BOUNCE_TYPES = new Map([
['__fxa__unmapped', 0], // a bounce type we don't yet recognize
['Permanent', 1], // Hard

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -40,7 +40,9 @@
"newrelic": "4.10.0",
"raven": "2.6.4",
"request": "2.88.0",
"restify": "7.2.2",
"restify": "7.2.2"
},
"optionalDependencies": {
"scrypt-hash": "1.1.14"
},
"devDependencies": {

0 comments on commit 932f2dd

Please sign in to comment.