npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safeguard

v0.0.2

Published

Safeguard makes cryptography easier in node.js by adding helpful functionality and simplifying the already existing crypto library.

Downloads

9

Readme

Safeguard

Safeguard makes hashing with crypto easier and more convenient.

Easier

Safeguard takes care of the cryptography settings so the API is simple.

var safeguard = require('safeguard');

// Hash some text
safeguard.hasher(text, function(err, hash) {
  // Do some things and stuff...
});

// Compare some text to the hash
safeguard.compareToHash(text, hash, function(err, isMatch) {
  // Do some things and stuff...
});

Convenient

The crypto settings and salt are encapsulated within each hash so you don't have to manage them. Unlike in crypto where you have to define them each time.

// Ewww... Yuk... Gross...
crypto.pbkdf2(password, salt, iterations, keySize, function(err, hash) {
  // Do some things and stuff...
}

Since every hash string contains the settings used to create it you can change the default crypto settings at anytime without having to worry about tracking the old settings for previously hashed values.

Install Safeguard using npm and save it as a dependency in your package.json.

npm install safeguard --save

You can require Safeguard just like every other node.js module.

var safeguard = require('safeguard');

API

compareToHash

Compare a hash value (created by safeguard) to a plain text string.

| Parameter | Type | Description | | --------- | ---- | ----------- | | text | String | The plain text to compare to the hash | | hash | String | The hash to compare the plain text to | | cb | Method | A callback method that accepts an error and the boolean result of the comparison |

safeguard.compareToHash(text, hash, function(err, isMatch) {
  // Do some things and stuff...
});

hasher

Hash a plain text string.

| Parameter | Type | Description | | --------- | ---- | ----------- | | text | String | The plain text to hash | | cb | Method | A callback method that accepts an error and the hash string value |

safeguard.hasher(text, function(err, hash) {
  // Do some things and stuff...
});

Constructor and Setters

You can configure Safeguard with the following attributes by passing them into the constructor or using the setter methods.

Constructor

The constructor accepts the following parameters:

| Parameter | Type | Description | | --------- | ---- | ----------- | | config | Object | Any configuration that will override the default behavior. | | log | Object | A seedio-log instance. | | error | Object | An error instance that contains methods to build errors produced by safeguard. |

Example of how you might use each parameter:

var config = {
  iterations: 20000,
  keyLength: 256
};

var log = require('seedio-log')({
  databaseLog: true,
  mongoose: require('mongoose'),
  name: 'MyAppName'
});

var error = {
  build: function(message, code) {
    var err = new Error(message);
    err.status = code || 400;
    return err;
  }
}

var safeguard = require(safeguard)(config, log, error);

setLog

Configure or pass in a seedio-log reference. See the seedio-log github for documentation.

| Parameter | Type | Description | | --------- | ---- | ----------- | | config | Object | Any configuration that will override the default behavior. | | log | Object | A seedio-log instance. | | error | Object | An error instance that contains methods to build errors produced by safeguard. |

Example of configuring the default log:

var safeguard = require('safeguard');

safeguard.setLog({ error: false });

Example of passing in an existing log reference.

var safeguard = require('safeguard');

var log = require('seedio-log')({
  databaseLog: true,
  mongoose: require('mongoose'),
  name: 'MyAppName'
});

safeguard.setLog(undefined, log);
```## setError
You can override the default error message building function.  By default all errors contain an HTTP error code in the status attribute.

```javascript
var safeguard = require('safeguard');

var error = {
  build: function(message, code) {
    var err = new Error(message);
    err.status = code || 500;
    return err;
  }
}

safeguard.setError(error);

setConfig

Configure safeguard by overriding the default configuration object.

var safeguard = require('safeguard');

safeguard.setConfig({ crypto: { iterations: 20000 } });

Configuration Object

The following attributes can be included in the configuration object to override the default behavior of Safeguard.

| Parameter | Type | Default | Description | | --------- | ---- | ------- | ----------- | | crypto | Object | n/a | Settings related to the node.js crypto library. | | crypto.defaultPlainTextLength | Number | undefined | When defined and an invalid string is hashed using the hasher, a random string of the specified size will be generated. | | crypto.iterations | Number | 10,000 | Number of times crypto iterates while hashing. | | crypto.keyLength | Number | 128 | Length of the text's hash value. | | crypto.saltLength | Number | 64 | Length of the hash's salt. |

Note: Choose iterations to satisfy the formula v-2^(n-1) > f-p

Example Default Config

{
  crypto: {
    defaultPlainTextLength: undefined,
    iterations: 10000,
    keyLength: 64,
    saltLength: 64
  }
}