index.js
function hash(password, options) {
  options = options || {};
  const blocksize = options.blocksize || defaults.blocksize;
  const cost = options.cost || defaults.cost;
  const parallelism = options.parallelism || defaults.parallelism;
  const saltSize = options.saltSize || defaults.saltSize;

  // Blocksize Validation
  if (typeof blocksize !== 'number' || !Number.isInteger(blocksize)) {
    return Promise.reject(
      new TypeError("The 'blocksize' option must be an integer")
    );
  }
  if (blocksize < 1 || blocksize > MAX_UINT32) {
    return Promise.reject(
      new TypeError(
        `The 'blocksize' option must be in the range (1 <= blocksize <= ${MAX_UINT32})`
      )
    );
  }

  // Cost Validation
  if (typeof cost !== 'number' || !Number.isInteger(cost)) {
    return Promise.reject(
      new TypeError("The 'cost' option must be an integer")
    );
  }
  const maxcost = (128 * blocksize) / 8 - 1;
  if (cost < 2 || cost > maxcost) {
    return Promise.reject(
      new TypeError(
        `The 'cost' option must be in the range (1 <= cost <= ${maxcost})`
      )
    );
  }

  // Parallelism Validation
  if (typeof parallelism !== 'number' || !Number.isInteger(parallelism)) {
    return Promise.reject(
      new TypeError("The 'parallelism' option must be an integer")
    );
  }
  const maxpar = Math.floor(((Math.pow(2, 32) - 1) * 32) / (128 * blocksize));
  if (parallelism < 1 || parallelism > maxpar) {
    return Promise.reject(
      new TypeError(
        `The 'parallelism' option must be in the range (1 <= parallelism <= ${maxpar})`
      )
    );
  }

  // Salt Size Validation
  if (saltSize < 8 || saltSize > 1024) {
    return Promise.reject(
      new TypeError(
        "The 'saltSize' option must be in the range (8 <= saltSize <= 1023)"
      )
    );
  }

  const params = {
    N: Math.pow(2, cost),
    r: blocksize,
    p: parallelism
  };
  const keylen = 32;

  return gensalt(saltSize).then(salt => {
    return scrypt.hash(password, params, keylen, salt).then(hash => {
      const phcstr = phc.serialize({
        id: 'scrypt',
        params: {
          ln: cost,
          r: blocksize,
          p: parallelism
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash(password, options) {
  options = options || {};
  const blocksize = options.blocksize || defaults.blocksize;
  const cost = options.cost || defaults.cost;
  const parallelism = options.parallelism || defaults.parallelism;
  const saltSize = options.saltSize || defaults.saltSize;

  // Blocksize Validation
  if (typeof blocksize !== 'number' || !Number.isInteger(blocksize)) {
    return Promise.reject(
      new TypeError("The 'blocksize' option must be an integer")
    );
  }
  if (blocksize < 1 || blocksize > MAX_UINT32) {
    return Promise.reject(
      new TypeError(
        `The 'blocksize' option must be in the range (1 <= blocksize <= ${MAX_UINT32})`
      )
    );
  }

  // Cost Validation
  if (typeof cost !== 'number' || !Number.isInteger(cost)) {
    return Promise.reject(
      new TypeError("The 'cost' option must be an integer")
    );
  }
  const maxcost = (128 * blocksize) / 8 - 1;
  if (cost < 2 || cost > maxcost) {
    return Promise.reject(
      new TypeError(
        `The 'cost' option must be in the range (1 <= cost <= ${maxcost})`
      )
    );
  }

  // Parallelism Validation
  if (typeof parallelism !== 'number' || !Number.isInteger(parallelism)) {
    return Promise.reject(
      new TypeError("The 'parallelism' option must be an integer")
    );
  }
  const maxpar = Math.floor(((Math.pow(2, 32) - 1) * 32) / (128 * blocksize));
  if (parallelism < 1 || parallelism > maxpar) {
    return Promise.reject(
      new TypeError(
        `The 'parallelism' option must be in the range (1 <= parallelism <= ${maxpar})`
      )
    );
  }

  // Salt Size Validation
  if (saltSize < 8 || saltSize > 1024) {
    return Promise.reject(
      new TypeError(
        "The 'saltSize' option must be in the range (8 <= saltSize <= 1023)"
      )
    );
  }

  const params = {
    N: Math.pow(2, cost),
    r: blocksize,
    p: parallelism
  };
  const keylen = 32;

  return gensalt(saltSize).then(salt => {
    return scrypt.hash(password, params, keylen, salt).then(hash => {
      const phcstr = phc.serialize({
        id: 'scrypt',
        params: {
          ln: cost,
          r: blocksize,
          p: parallelism
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash ( password , options ) { options = options || { } ; const blocksize = options . blocksize || defaults . blocksize ; const cost = options . cost || defaults . cost ; const parallelism = options . parallelism || defaults . parallelism ; const saltSize = options . saltSize || defaults . saltSize ; // Blocksize Validation if ( typeof blocksize !== 'number' || ! Number . isInteger ( blocksize ) ) { return Promise . reject ( new TypeError ( "The 'blocksize' option must be an integer" ) ) ; } if ( blocksize < 1 || blocksize > MAX_UINT32 ) { return Promise . reject ( new TypeError ( ` ${ MAX_UINT32 } ` ) ) ; } // Cost Validation if ( typeof cost !== 'number' || ! Number . isInteger ( cost ) ) { return Promise . reject ( new TypeError ( "The 'cost' option must be an integer" ) ) ; } const maxcost = ( 128 * blocksize ) / 8 - 1 ; if ( cost < 2 || cost > maxcost ) { return Promise . reject ( new TypeError ( ` ${ maxcost } ` ) ) ; } // Parallelism Validation if ( typeof parallelism !== 'number' || ! Number . isInteger ( parallelism ) ) { return Promise . reject ( new TypeError ( "The 'parallelism' option must be an integer" ) ) ; } const maxpar = Math . floor ( ( ( Math . pow ( 2 , 32 ) - 1 ) * 32 ) / ( 128 * blocksize ) ) ; if ( parallelism < 1 || parallelism > maxpar ) { return Promise . reject ( new TypeError ( ` ${ maxpar } ` ) ) ; } // Salt Size Validation if ( saltSize < 8 || saltSize > 1024 ) { return Promise . reject ( new TypeError ( "The 'saltSize' option must be in the range (8 <= saltSize <= 1023)" ) ) ; } const params = { N : Math . pow ( 2 , cost ) , r : blocksize , p : parallelism } ; const keylen = 32 ; return gensalt ( saltSize ) . then ( salt => { return scrypt . hash ( password , params , keylen , salt ) . then ( hash => { const phcstr = phc . serialize ( { id : 'scrypt' , params : { ln : cost , r : blocksize , p : parallelism } , salt , hash } ) ; return phcstr ; } ) ; } ) ; }
Computes the hash string of the given password in the PHC format using scrypt
package.
@public
@param  {string} password The password to hash.
@param  {Object} [options] Optional configurations related to the hashing
function.
@param  {number} [options.blocksize=8] Optional amount of memory to use in
kibibytes.
Must be an integer within the range (`8` <= `memory` <= `2^32-1`).
@param  {number} [options.cost=15] Optional CPU/memory cost parameter.
Must be an integer power of 2 within the range
(`2` <= `cost` <= `2^((128 * blocksize) / 8) - 1`).
@param  {number} [options.parallelism=1] Optional degree of parallelism to
use.
Must be an integer within the range
(`1` <= `parallelism` <= `((2^32-1) * 32) / (128 * blocksize)`).
@return {Promise.<string>} The generated secure hash string in the PHC
format.
Computes the hash string of the given password in the PHC format using scrypt package .