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

bcrypt-schema

v0.1.3

Published

Encrypt schema fields. Use it as a mongoose plugin or as a standalone module.

Downloads

3

Readme

bcrypt-schema

Build Status Dependency Status devDependency Status Coverage Status Codacy Badge

NPM package for adding encryption on schema fields. Can be used as a Mongoose plugin. Designed for use with Node.js and installable via npm install bcrypt-schema

Quick examples

  • As a mongoose plugin
var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

var userSchema = new mongoose.Schema({
    username: String,
    password: {
        type: String,
        select: false
    }
})

userSchema.plugin(encryption, 'password')
  • As a standalone module
// You can use the `verify(value, hash, next)` and `set(value, hashField, next)` methods
// on any object as schema instance methods
// Suppose we use mongoose again the example above would look like the following
var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema')

var userSchema = new mongoose.Schema({
    username: String,
    password: {
        type: String,
        select: false
    }
})

userSchema.methods.verifyPassword = encryption.verify
userSchema.methods.setPassword = encryption.set

// ...
// Before saving a document
// Suppose we keep the plain password as a virual property
userSchema.virtual('plainPassword')
    .get(function() {
        return this._plainPassword
    })
    .set(function(password) {
        this._plainPassword = password
    })

userSchema.pre('save', function(callback) {

    // Password has not changed
    if (!this.isModified('plainPassword') && !this.isNew) {
        return callback()
    }

    //Password has changed or this is a new user
    this.setPassword(this.plainPassword, 'password', callback)
})

// ...
// Later having a user document
user.verifyPassword(password, user.password, function(err, isVerified) {
  // Handle verification result in this callback
}

Documentation

Compares the value of value with the given hash using bcrypt.compare(). The next parameter is a callback function.

Arguments

  • value - The plain value used in comparison.
  • hash - A hash to compare against.
  • next(err, isVeified) - A callback which is called after the comparison has finished.

Hasheh the given value using bcrypt's genSalt and hash and assigns it to this[hashField].

Arguments

  • value - The plain value to hash.
  • hashField - The name of the property to be set.
  • saltIterations - Optional. A parameter passed to bcrypt.genSalt. Defaults to 10.
  • next(err) - A callback which is called after hashing has finished.

Accepts a mongoose-like schema object and a set of options. Can be used as a mongoose plugin.

Arguments

  • schema - A mongoose-like schema object.
  • options- An object containing plugin options
    • field - The name of the field to be encrypted
    • verify - The name of the verify method attached as an instance method on the schema.methods
    • set - The name of the set method attached as an instance method on the schema.methods
    • saltIterations - A parameter passed to bcrypt.genSalt. Defaults to 10

Example

var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

// Define a schema
// ...

schema.plugin(encryption, {
        field: 'password',
        verify: 'customVerifyName',
        set: 'customSetName',
        saltIterations: 100
    })

After the plugin call you can use the customVerifyName and customSetName methods as instance methods.

setEncryption(schema, fieldName)

This is a short hand overload of the setEncryption method which auto-generates verify and set method names using the value of the fieldName parameter. Method names are generated adding the PascalCase version of the fieldName to the strings verify and set.

For instance, if fieldName equals 'password' the generated methods are named verifyPassword and setPassword.

Arguments

  • schema - A mongoose-like schema object
  • fieldName - The name of the field to be encrypted

Example

var mongoose = require('mongoose'),
    encryption = require('bcrypt-schema').setEncryption

// Define a schema
// ...

schema.plugin(encryption, 'password')