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

  // Rounds Validation
  if (typeof rounds !== 'number' || !Number.isInteger(rounds)) {
    return Promise.reject(
      new TypeError("The 'rounds' option must be an integer")
    );
  }
  if (rounds < 4 || rounds > 31) {
    return Promise.reject(
      new TypeError(
        `The 'rounds' option must be in the range (4 <= rounds <= 31)`
      )
    );
  }

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

  return gensalt(saltSize).then(salt => {
    const bb64salt = bb64.encode(salt);
    const padrounds = rounds > 9 ? Number(rounds) : '0' + rounds;
    const decver = String.fromCharCode(version);
    const parstr = `$2${decver}$${padrounds}$${bb64salt}`;
    return bcrypt.hash(password, parstr).then(enchash => {
      const hash = bb64.decode(enchash.split(parstr)[1]);
      const phcstr = phc.serialize({
        id: 'bcrypt',
        version,
        params: {
          r: rounds
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash(password, options) {
  options = options || {};
  const rounds = options.rounds || defaults.rounds;
  const saltSize = options.saltSize || defaults.saltSize;
  const version = versions[versions.length - 1];

  // Rounds Validation
  if (typeof rounds !== 'number' || !Number.isInteger(rounds)) {
    return Promise.reject(
      new TypeError("The 'rounds' option must be an integer")
    );
  }
  if (rounds < 4 || rounds > 31) {
    return Promise.reject(
      new TypeError(
        `The 'rounds' option must be in the range (4 <= rounds <= 31)`
      )
    );
  }

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

  return gensalt(saltSize).then(salt => {
    const bb64salt = bb64.encode(salt);
    const padrounds = rounds > 9 ? Number(rounds) : '0' + rounds;
    const decver = String.fromCharCode(version);
    const parstr = `$2${decver}$${padrounds}$${bb64salt}`;
    return bcrypt.hash(password, parstr).then(enchash => {
      const hash = bb64.decode(enchash.split(parstr)[1]);
      const phcstr = phc.serialize({
        id: 'bcrypt',
        version,
        params: {
          r: rounds
        },
        salt,
        hash
      });
      return phcstr;
    });
  });
}
function hash ( password , options ) { options = options || { } ; const rounds = options . rounds || defaults . rounds ; const saltSize = options . saltSize || defaults . saltSize ; const version = versions [ versions . length - 1 ] ; // Rounds Validation if ( typeof rounds !== 'number' || ! Number . isInteger ( rounds ) ) { return Promise . reject ( new TypeError ( "The 'rounds' option must be an integer" ) ) ; } if ( rounds < 4 || rounds > 31 ) { return Promise . reject ( new TypeError ( ` ` ) ) ; } // Salt Size Validation if ( saltSize < 8 || saltSize > 1024 ) { return Promise . reject ( new TypeError ( "The 'saltSize' option must be in the range (8 <= saltSize <= 1023)" ) ) ; } return gensalt ( saltSize ) . then ( salt => { const bb64salt = bb64 . encode ( salt ) ; const padrounds = rounds > 9 ? Number ( rounds ) : '0' + rounds ; const decver = String . fromCharCode ( version ) ; const parstr = ` ${ decver } ${ padrounds } ${ bb64salt } ` ; return bcrypt . hash ( password , parstr ) . then ( enchash => { const hash = bb64 . decode ( enchash . split ( parstr ) [ 1 ] ) ; const phcstr = phc . serialize ( { id : 'bcrypt' , version , params : { r : rounds } , salt , hash } ) ; return phcstr ; } ) ; } ) ; }
Computes the hash string of the given password in the PHC format using bcrypt
package.
@public
@param  {string} password The password to hash.
@param  {Object} [options] Optional configurations related to the hashing
function.
@param  {number} [options.rounds=10] Optional
Must be an integer within the range (`4` <= `rounds` <= `31`).
@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 bcrypt package .