index.js
function deserialize(phcstr) {
  if (typeof phcstr !== 'string' || phcstr === '') {
    throw new TypeError('pchstr must be a non-empty string');
  }
  if (phcstr[0] !== '$') {
    throw new TypeError('pchstr must contain a $ as first char');
  }
  const fields = phcstr.split('$');
  // Remove first empty $
  fields.shift();

  // Parse Fields
  let maxf = 5;
  if (!versionRegex.test(fields[1])) maxf--;
  if (fields.length > maxf) {
    throw new TypeError(
      `pchstr contains too many fileds: ${fields.length}/${maxf}`
    );
  }

  // Parse Identifier
  const id = fields.shift();
  if (!idRegex.test(id)) {
    throw new TypeError(`id must satisfy ${idRegex}`);
  }

  let version;
  // Parse Version
  if (versionRegex.test(fields[0])) {
    version = parseInt(fields.shift().match(versionRegex)[1], 10);
  }

  let hash;
  let salt;
  if (b64Regex.test(fields[fields.length - 1])) {
    if (fields.length > 1 && b64Regex.test(fields[fields.length - 2])) {
      // Parse Hash
      hash = Buffer.from(fields.pop(), 'base64');
      // Parse Salt
      salt = Buffer.from(fields.pop(), 'base64');
    } else {
      // Parse Salt
      salt = Buffer.from(fields.pop(), 'base64');
    }
  }

  // Parse Parameters
  let params;
  if (fields.length > 0) {
    const parstr = fields.pop();
    params = keyValtoObj(parstr);
    if (!objectKeys(params).every(p => nameRegex.test(p))) {
      throw new TypeError(`params names must satisfy ${nameRegex}`);
    }
    const pv = objectValues(params);
    if (!pv.every(v => valueRegex.test(v))) {
      throw new TypeError(`params values must satisfy ${valueRegex}`);
    }
    const pk = objectKeys(params);
    // Convert Decimal Strings into Numbers
    pk.forEach(k => {
      params[k] = decimalRegex.test(params[k])
        ? parseInt(params[k], 10)
        : params[k];
    });
  }

  if (fields.length > 0) {
    throw new TypeError(`pchstr contains unrecognized fileds: ${fields}`);
  }

  // Build the output object
  const phcobj = {id};
  if (version) phcobj.version = version;
  if (params) phcobj.params = params;
  if (salt) phcobj.salt = salt;
  if (hash) phcobj.hash = hash;

  return phcobj;
}
function deserialize(phcstr) {
  if (typeof phcstr !== 'string' || phcstr === '') {
    throw new TypeError('pchstr must be a non-empty string');
  }
  if (phcstr[0] !== '$') {
    throw new TypeError('pchstr must contain a $ as first char');
  }
  const fields = phcstr.split('$');
  // Remove first empty $
  fields.shift();

  // Parse Fields
  let maxf = 5;
  if (!versionRegex.test(fields[1])) maxf--;
  if (fields.length > maxf) {
    throw new TypeError(
      `pchstr contains too many fileds: ${fields.length}/${maxf}`
    );
  }

  // Parse Identifier
  const id = fields.shift();
  if (!idRegex.test(id)) {
    throw new TypeError(`id must satisfy ${idRegex}`);
  }

  let version;
  // Parse Version
  if (versionRegex.test(fields[0])) {
    version = parseInt(fields.shift().match(versionRegex)[1], 10);
  }

  let hash;
  let salt;
  if (b64Regex.test(fields[fields.length - 1])) {
    if (fields.length > 1 && b64Regex.test(fields[fields.length - 2])) {
      // Parse Hash
      hash = Buffer.from(fields.pop(), 'base64');
      // Parse Salt
      salt = Buffer.from(fields.pop(), 'base64');
    } else {
      // Parse Salt
      salt = Buffer.from(fields.pop(), 'base64');
    }
  }

  // Parse Parameters
  let params;
  if (fields.length > 0) {
    const parstr = fields.pop();
    params = keyValtoObj(parstr);
    if (!objectKeys(params).every(p => nameRegex.test(p))) {
      throw new TypeError(`params names must satisfy ${nameRegex}`);
    }
    const pv = objectValues(params);
    if (!pv.every(v => valueRegex.test(v))) {
      throw new TypeError(`params values must satisfy ${valueRegex}`);
    }
    const pk = objectKeys(params);
    // Convert Decimal Strings into Numbers
    pk.forEach(k => {
      params[k] = decimalRegex.test(params[k])
        ? parseInt(params[k], 10)
        : params[k];
    });
  }

  if (fields.length > 0) {
    throw new TypeError(`pchstr contains unrecognized fileds: ${fields}`);
  }

  // Build the output object
  const phcobj = {id};
  if (version) phcobj.version = version;
  if (params) phcobj.params = params;
  if (salt) phcobj.salt = salt;
  if (hash) phcobj.hash = hash;

  return phcobj;
}
function deserialize ( phcstr ) { if ( typeof phcstr !== 'string' || phcstr === '' ) { throw new TypeError ( 'pchstr must be a non-empty string' ) ; } if ( phcstr [ 0 ] !== '$' ) { throw new TypeError ( 'pchstr must contain a $ as first char' ) ; } const fields = phcstr . split ( '$' ) ; // Remove first empty $ fields . shift ( ) ; // Parse Fields let maxf = 5 ; if ( ! versionRegex . test ( fields [ 1 ] ) ) maxf -- ; if ( fields . length > maxf ) { throw new TypeError ( ` ${ fields . length } ${ maxf } ` ) ; } // Parse Identifier const id = fields . shift ( ) ; if ( ! idRegex . test ( id ) ) { throw new TypeError ( ` ${ idRegex } ` ) ; } let version ; // Parse Version if ( versionRegex . test ( fields [ 0 ] ) ) { version = parseInt ( fields . shift ( ) . match ( versionRegex ) [ 1 ] , 10 ) ; } let hash ; let salt ; if ( b64Regex . test ( fields [ fields . length - 1 ] ) ) { if ( fields . length > 1 && b64Regex . test ( fields [ fields . length - 2 ] ) ) { // Parse Hash hash = Buffer . from ( fields . pop ( ) , 'base64' ) ; // Parse Salt salt = Buffer . from ( fields . pop ( ) , 'base64' ) ; } else { // Parse Salt salt = Buffer . from ( fields . pop ( ) , 'base64' ) ; } } // Parse Parameters let params ; if ( fields . length > 0 ) { const parstr = fields . pop ( ) ; params = keyValtoObj ( parstr ) ; if ( ! objectKeys ( params ) . every ( p => nameRegex . test ( p ) ) ) { throw new TypeError ( ` ${ nameRegex } ` ) ; } const pv = objectValues ( params ) ; if ( ! pv . every ( v => valueRegex . test ( v ) ) ) { throw new TypeError ( ` ${ valueRegex } ` ) ; } const pk = objectKeys ( params ) ; // Convert Decimal Strings into Numbers pk . forEach ( k => { params [ k ] = decimalRegex . test ( params [ k ] ) ? parseInt ( params [ k ] , 10 ) : params [ k ] ; } ) ; } if ( fields . length > 0 ) { throw new TypeError ( ` ${ fields } ` ) ; } // Build the output object const phcobj = { id } ; if ( version ) phcobj . version = version ; if ( params ) phcobj . params = params ; if ( salt ) phcobj . salt = salt ; if ( hash ) phcobj . hash = hash ; return phcobj ; }
Parses data from a PHC string.
@param  {string} phcstr A PHC string to parse.
@return {Object} The object containing the data parsed from the PHC string.
Parses data from a PHC string .