1.0.0 • Published 4 years ago

@phc/format v1.0.0

Weekly downloads
46,509
License
MIT
Repository
github
Last release
4 years ago

Motivation

The PHC String Format is an attempt to specify a common hash string format that’s a restricted & well defined subset of the Modular Crypt Format. New hashes are strongly encouraged to adhere to the PHC specification, rather than the much looser Modular Crypt Format.

Install

npm install --save @phc/format

Usage

const phc = require('@phc/format');

const phcobj = {
  id: 'pbkdf2-sha256',
  params: {i: 6400},
  salt: Buffer.from('0ZrzXitFSGltTQnBWOsdAw', 'base64'),
  hash: Buffer.from('Y11AchqV4b0sUisdZd0Xr97KWoymNE0LNNrnEgY4H9M', 'base64'),
};

const phcstr = "$pbkdf2-sha256$i=6400$0ZrzXitFSGltTQnBWOsdAw$Y11AchqV4b0sUisdZd0Xr97KWoymNE0LNNrnEgY4H9M";

phc.serialize(phcobj);
// => phcstr

phc.deserialize(phcstr);
// => phcobj

You can also pass an optional version parameter.

const phc = require('@phc/format');

const phcobj = {
  id: 'argon2i',
  version: 19,
  params: {
    m: 120,
    t: 5000,
    p: 2
  },
  salt: Buffer.from('iHSDPHzUhPzK7rCcJgOFfg', 'base64'),
  hash: Buffer.from('J4moa2MM0/6uf3HbY2Tf5Fux8JIBTwIhmhxGRbsY14qhTltQt+Vw3b7tcJNEbk8ium8AQfZeD4tabCnNqfkD1g', 'base64'),
};

const phcstr = "$argon2i$v=19$m=120,t=5000,p=2$iHSDPHzUhPzK7rCcJgOFfg$J4moa2MM0/6uf3HbY2Tf5Fux8JIBTwIhmhxGRbsY14qhTltQt+Vw3b7tcJNEbk8ium8AQfZeD4tabCnNqfkD1g";

phc.serialize(phcobj);
// => phcstr

phc.deserialize(phcstr);
// => phcobj

API

TOC

serialize(opts) ⇒ string

Generates a PHC string using the data provided.

Kind: global function
Returns: string - The hash string adhering to the PHC format.

ParamTypeDescription
optsObjectObject that holds the data needed to generate the PHC string.
opts.idstringSymbolic name for the function.
opts.versionNumberThe version of the function.
opts.paramsObjectParameters of the function.
opts.saltBufferThe salt as a binary buffer.
opts.hashBufferThe hash as a binary buffer.

deserialize(phcstr) ⇒ Object

Parses data from a PHC string.

Kind: global function
Returns: Object - The object containing the data parsed from the PHC string.

ParamTypeDescription
phcstrstringA PHC string to parse.

Contributing

Contributions are REALLY welcome and if you find a security flaw in this code, PLEASE report it.
Please check the contributing guidelines for more details. Thanks!

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the license file for details.