index.js
function hash(password, options) {
  options = options || {};
  let variant = options.variant || defaults.variant;
  const iterations = options.iterations || defaults.iterations;
  const memory = options.memory || defaults.memory;
  const parallelism = options.parallelism || defaults.parallelism;
  const saltSize = options.saltSize || defaults.saltSize;
  const version = versions[versions.length - 1];

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

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

  // Memory Validation
  if (typeof memory !== 'number' || !Number.isInteger(memory)) {
    return Promise.reject(
      new TypeError("The 'memory' option must be an integer")
    );
  }
  const minmem = 8 * parallelism;
  if (memory < minmem || memory > MAX_UINT32) {
    return Promise.reject(
      new TypeError(
        `The 'memory' option must be in the range (${minmem} <= memory <= ${MAX_UINT32})`
      )
    );
  }

  // Variant Validation
  if (typeof variant !== 'string') {
    return Promise.reject(
      new TypeError("The 'variant' option must be a string")
    );
  }
  variant = variant.toLowerCase();
  if (!Object.prototype.hasOwnProperty.call(variants, variant)) {
    return Promise.reject(
      new TypeError(
        `The 'variant' option must be one of: ${Object.keys(variants)}`
      )
    );
  }

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

  return gensalt(saltSize).then(salt => {
    const params = {
      version,
      type: variants[variant],
      timeCost: iterations,
      memoryCost: memory,
      parallelism,
      salt,
      raw: true
    };
    return argon2.hash(password, params).then(hash => {
      const phcstr = phc.serialize({
        id: `argon2${variant}`,
        version,
        params: {
          t: iterations,
          m: memory,
          p: parallelism
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash(password, options) {
  options = options || {};
  let variant = options.variant || defaults.variant;
  const iterations = options.iterations || defaults.iterations;
  const memory = options.memory || defaults.memory;
  const parallelism = options.parallelism || defaults.parallelism;
  const saltSize = options.saltSize || defaults.saltSize;
  const version = versions[versions.length - 1];

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

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

  // Memory Validation
  if (typeof memory !== 'number' || !Number.isInteger(memory)) {
    return Promise.reject(
      new TypeError("The 'memory' option must be an integer")
    );
  }
  const minmem = 8 * parallelism;
  if (memory < minmem || memory > MAX_UINT32) {
    return Promise.reject(
      new TypeError(
        `The 'memory' option must be in the range (${minmem} <= memory <= ${MAX_UINT32})`
      )
    );
  }

  // Variant Validation
  if (typeof variant !== 'string') {
    return Promise.reject(
      new TypeError("The 'variant' option must be a string")
    );
  }
  variant = variant.toLowerCase();
  if (!Object.prototype.hasOwnProperty.call(variants, variant)) {
    return Promise.reject(
      new TypeError(
        `The 'variant' option must be one of: ${Object.keys(variants)}`
      )
    );
  }

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

  return gensalt(saltSize).then(salt => {
    const params = {
      version,
      type: variants[variant],
      timeCost: iterations,
      memoryCost: memory,
      parallelism,
      salt,
      raw: true
    };
    return argon2.hash(password, params).then(hash => {
      const phcstr = phc.serialize({
        id: `argon2${variant}`,
        version,
        params: {
          t: iterations,
          m: memory,
          p: parallelism
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash ( password , options ) { options = options || { } ; let variant = options . variant || defaults . variant ; const iterations = options . iterations || defaults . iterations ; const memory = options . memory || defaults . memory ; const parallelism = options . parallelism || defaults . parallelism ; const saltSize = options . saltSize || defaults . saltSize ; const version = versions [ versions . length - 1 ] ; // Iterations Validation if ( typeof iterations !== 'number' || ! Number . isInteger ( iterations ) ) { return Promise . reject ( new TypeError ( "The 'iterations' option must be an integer" ) ) ; } if ( iterations < 1 || iterations > MAX_UINT32 ) { return Promise . reject ( new TypeError ( ` ${ MAX_UINT32 } ` ) ) ; } // Parallelism Validation if ( typeof parallelism !== 'number' || ! Number . isInteger ( parallelism ) ) { return Promise . reject ( new TypeError ( "The 'parallelism' option must be an integer" ) ) ; } if ( parallelism < 1 || parallelism > MAX_UINT24 ) { return Promise . reject ( new TypeError ( ` ${ MAX_UINT24 } ` ) ) ; } // Memory Validation if ( typeof memory !== 'number' || ! Number . isInteger ( memory ) ) { return Promise . reject ( new TypeError ( "The 'memory' option must be an integer" ) ) ; } const minmem = 8 * parallelism ; if ( memory < minmem || memory > MAX_UINT32 ) { return Promise . reject ( new TypeError ( ` ${ minmem } ${ MAX_UINT32 } ` ) ) ; } // Variant Validation if ( typeof variant !== 'string' ) { return Promise . reject ( new TypeError ( "The 'variant' option must be a string" ) ) ; } variant = variant . toLowerCase ( ) ; if ( ! Object . prototype . hasOwnProperty . call ( variants , variant ) ) { return Promise . reject ( new TypeError ( ` ${ Object . keys ( variants ) } ` ) ) ; } // Salt Size Validation if ( saltSize < 8 || saltSize > 1024 ) { return Promise . reject ( new TypeError ( "The 'saltSize' option must be in the range (8 <= parallelism <= 1023)" ) ) ; } return gensalt ( saltSize ) . then ( salt => { const params = { version , type : variants [ variant ] , timeCost : iterations , memoryCost : memory , parallelism , salt , raw : true } ; return argon2 . hash ( password , params ) . then ( hash => { const phcstr = phc . serialize ( { id : ` ${ variant } ` , version , params : { t : iterations , m : memory , p : parallelism } , salt , hash } ) ; return phcstr ; } ) ; } ) ; }
Computes the hash string of the given password in the PHC format using argon2
package.
@public
@param  {string} password The password to hash.
@param  {Object} [options] Optional configurations related to the hashing
function.
@param  {number} [options.variant=id] Optinal variant of argon2 to use.
Can be one of [`'d'`, `'i'`, `'id'`] for argon2d, argon2i and argon2id
respectively.
@param  {number} [options.iterations=3] Optional number of iterations to use.
Must be an integer within the range (`1` <= `iterations` <= `2^32-1`).
@param  {number} [options.memory=4096] Optional amount of memory to use in
kibibytes.
Must be an integer within the range (`8` <= `memory` <= `2^32-1`).
@param  {number} [options.parallelism=1] Optional degree of parallelism to
use.
Must be an integer within the range (`1` <= `parallelism` <= `2^24-1`).
@param  {number} [options.saltSize=16] Optional number of bytes to use when
autogenerating new salts.
Must be an integer within the range (`1` <= `saltSize` <= `2^10-1`).
@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 argon2 package .