Giter VIP home page Giter VIP logo

crypto-password-helper's Introduction

crypto-password-helper NPM version

Helper for encrypting and generating password using crypto in node.js. The encryption is made using cryptos pbkdf2 method. Salt, iterations and digest is automatically added to the password hash so no need to save anything but the returned hash.

Installation

Install via NPM using command:

$ npm install --save crypto-password-helper

Usage

The hash is always returned via a promise as a string (URL-safe). Two simple methods are available from the password helper, usage below:

var password = require('crypto-password-helper');

// Using default configuration
password.encrypt(plainPassword).then(hash => {          // using default settings
    console.log(hash);                                  // save hash to user/database
}).catch(err => { throw err; });                        // handle internal server error. (crypto error)

// Using your own configuration
password.encrypt(plainPassword, config).then(hash => {  // using your own configuration
    console.log(hash);
}).catch(err => { throw err; });                        // handle internal server error. (crypto error)

// Generate password-hash synchronous. (with options) - throws on error
let hash = password.encryptSync(plainPassword, options);           

// Generate password-hash synchronous. (without options) - throws on error
let hash = password.encryptSync(plainPassword);

// Check if password match
password.compare(plainPassword, hashedPassword).then(isMatch => {
    if(isMatch) {                                       // isMatch is true if the plainPassword matches the hashedPassword, otherwise false.
        login();
    } else {
        unauthorized();
    }
}).catch(err => { throw err; });                        // Handle internal server error. (crypto error)

// Compare synchronous. (Throws on error)
let isMatch = password.compareSync(plainPassword, hashedPassword);

Configuration

There are four values that are configurable. See below for default configuration. All settings are adjustable.

let defaults = {
    // Number of iterations (higher number -> more secure but takes longer time to complete.)
    iterations: 612484,

    // Number of bytes for the password hash.
    hashSize: 32,

    // Number of bytes for the password salt.
    saltSize: 16,

    // The digest used to hash the password. See list of digests for crypto.
    digest: 'sha512'
}

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

License

MIT © Tommy Dronkers

crypto-password-helper's People

Contributors

snappyjs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.