Socket
Socket
Sign inDemoInstall

@phc/format

Package Overview
Dependencies
1
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @phc/format

PHC string format serializer/deserializer


Version published
Maintainers
1
Install size
48.3 kB
Created

Readme

Source

phc-format

Mac/Linux Build Status Windows Build status Codecov Coverage report Known Vulnerabilities Dependency Status
XO Code Style used AVA Test Runner used Istanbul Test Coverage used NI Scaffolding System used NP Release System used
Latest version on npm Project license

📝 PHC string format serializer/deserializer
Coded with ❤️ by Simone Primarosa.

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.

Do you believe that this is useful? Has it saved you time? Or maybe you simply like it?
If so, show your appreciation with a Star ⭐️.

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

Using the raw and strict parameters you can even serialize/deserialize PHC strings that does not strictly adhere to the 'standard', like the one used by argon2

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

const phcobj = {
  id: 'argon2i',
  raw: 'v=19', ← Note the v parameter
  params: {
    m: 120,
    t: 5000,
    p: 2
  },
  salt: Buffer.from('iHSDPHzUhPzK7rCcJgOFfg', 'base64'),
  hash: Buffer.from('J4moa2MM0/6uf3HbY2Tf5Fux8JIBTwIhmhxGRbsY14qhTltQt+Vw3b7tcJNEbk8ium8AQfZeD4tabCnNqfkD1g', 'base64'),
};
                         ↓ Note the v parameter
const phcstr = "$argon2i$v=19$m=120,t=5000,p=2$iHSDPHzUhPzK7rCcJgOFfg$J4moa2MM0/6uf3HbY2Tf5Fux8JIBTwIhmhxGRbsY14qhTltQt+Vw3b7tcJNEbk8ium8AQfZeD4tabCnNqfkD1g";

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

phc.deserialize(phcstr);
// => throws an error since there are more than 4 fields (a field is one $)

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

With the same philosophy you can even serialize/deserialize MCF formatted strings.

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

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

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

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

phc.deserialize(phcstr);
// => throws an error since the second field (a field is one $) is not a valid
// params string

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

API

TOC
serialize(opts)string

Generates a PHC string using the data provided.

deserialize(phcstr, [strict])Object

Parses data from a PHC string.

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.raw]stringAdditional raw data added after the identifier. It's here to support argon2 v parameter and to generate MCF formatted strings.
[opts.params]ObjectParameters of the function.
[opts.salt]BufferThe salt as a binary buffer.
[opts.hash]BufferThe hash as a binary buffer.

deserialize(phcstr, [strict]) ⇒ Object

Parses data from a PHC string.

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

ParamTypeDefaultDescription
phcstrstringA PHC string to parse.
[strict]booleantrueIf false does not throw an error if there is one filed not unrecognized. The content of the unrecognized filed will be stored in the raw property of the output object. This is useful to parse out of specs parameters like the 'v' present in the argon2 hash format or to parse MCF formatted strings.

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.

Keywords

FAQs

Last updated on 01 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc