Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bleskomat/scrypt

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bleskomat/scrypt

Node.js module that provides a wrapper API to node's built-in scrypt implementation.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

scrypt-node

Build Status

Node.js module that provides a wrapper API to node's built-in scrypt implementation.

Why not just use the built-in scrypt of Node.js? You could, but it's nice to have a portable serialization format that can be stored in a database or configuration file long-term without worrying about incompatibility when changing hashing options. An example of the serialization format used by this module:

$scrypt$1$14$iAoNah2WdPs7s2JZTd0Velb6ycQ=$ttq2cz7NoXNkAs6Nbl+TNKZsYFaEQJFcIWNTApiV67k=

$scrypt$ is the prefix and the $ symbol is used as a delimiter. The first value is the serialization format version - in this case 1. The second value is the cost exponent - in this case 14 meaning the cost is equal to 2^14 or 16384. The third value is the base64-encoded salt. And the fourth value is the base64-encoded derived key.

Installation

Add to your application via npm:

npm install @bleskomat/scrypt

Usage

Create a hash of a secret:

const scrypt = require('@bleskomat/scrypt');
const secret = 'super secret password';
const salt = scrypt.generateSalt();
scrypt.hash(secret, salt).then(result => {
	console.log(result);
	// $scrypt$1$14$iAoNah2WdPs7s2JZTd0Velb6ycQ=$ttq2cz7NoXNkAs6Nbl+TNKZsYFaEQJFcIWNTApiV67k=
});

The complete function signature is scrypt.hash(secret, salt, keylen, options). The keylen and options arguments are passed to crypto.scrypt. The default value for keylen is 32 bytes.

And scrypt.generateSalt(numBytes) where numBytes are the number of random bytes to generate. The default value for numBytes is 20.

Check if a secret matches a hash:

const scrypt = require('@bleskomat/scrypt');
const secret = 'super secret password';
const hash = '$scrypt$1$14$iAoNah2WdPs7s2JZTd0Velb6ycQ=$ttq2cz7NoXNkAs6Nbl+TNKZsYFaEQJFcIWNTApiV67k=';
scrypt.compare(secret, hash).then(result => {
	console.log(result ? 'OK' : 'DOES NOT MATCH');
});

Synchronous Usage

Synchronously create a hash:

const scrypt = require('@bleskomat/scrypt');
const secret = 'super secret password';
const salt = scrypt.generateSalt();
const result = scrypt.hashSync(secret, salt);
console.log(result);
// $scrypt$1$14$iAoNah2WdPs7s2JZTd0Velb6ycQ=$ttq2cz7NoXNkAs6Nbl+TNKZsYFaEQJFcIWNTApiV67k=

The complete function signature is scrypt.hashSync(secret, salt, keylen, options). The keylen and options arguments are passed to crypto.scryptSync. The default value for keylen is 32 bytes.

And scrypt.generateSalt(numBytes) where numBytes are the number of random bytes to generate. The default value for numBytes is 20.

Synchronously check if a secret matches a hash:

const scrypt = require('@bleskomat/scrypt');
const secret = 'super secret password';
const hash = '$scrypt$1$14$iAoNah2WdPs7s2JZTd0Velb6ycQ=$ttq2cz7NoXNkAs6Nbl+TNKZsYFaEQJFcIWNTApiV67k=';
const result = scrypt.compareSync(secret, hash);
console.log(result ? 'OK' : 'DOES NOT MATCH');

Tests

Run automated tests as follows:

npm test

Changelog

See CHANGELOG.md

License

This software is MIT licensed:

A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source. There are many variations of this license in use.

Keywords

FAQs

Package last updated on 01 Apr 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc