哈希和盐渍密码字段

时间:2017-10-10 17:44:25

标签: express hash mongoose bcrypt salt

我试图哈希并加密密码,但我收到了很多错误! 该代码有什么问题或者输入它的正确方法是什么?

user.js代码

Point p = The point you need to test
Point[] V = The vertices of the polygon as an array of Points
//Important to note: This version of the algorithm doesn't handle a closed polygon 
//if the last Point in `V` is not V[0] as well!
int n = V.Length - 1;

//Returns Integer value depending on the position 
//of the tested point and a line going through 2 other points.
//Returns >0 if Point is left of line <0 if Point is right of line and 0 if Point is on line.
public static int isLeft(Point P0, Point P1, Point P2)
{
    return ((P1.X - P0.X) * (P2.Y - P0.Y)
         - (P2.X - P0.X) * (P1.Y - P0.Y));
}

//Returns the Winding Number of the Point p against the Polygon V[n + 1]
//V[] should contain all of the vertices of the polygon, ending with V[0]
//n should be V.Length - 1;
//It returns 0 if the point is outside the polygon, and any other value (Positive or negative)  if it is inside
public static int wn_PnPoly(Point P, Point[] V, int n)
{
    int wn = 0;

    // loop through all edges of the polygon
    for (int i = 0; i < n; i++) 
    {  
        if (V[i].Y <= P.Y)
        {          
            if (V[i + 1].Y > P.Y)
            {     
                if (isLeft(V[i], V[i + 1], P) > 0)
                {  
                    ++wn;
                } 
            }
        }
        else
        {
            if (V[i + 1].Y <= P.Y)
            {
                if (isLeft(V[i], V[i + 1], P) < 0)
                {
                    --wn;
                }
            }
        }
    }
        return wn;
}

1 个答案:

答案 0 :(得分:0)

const mongoose = require('mongoose')
const schema = mongoose.Schema
const promise = require('bluebird')
const bcrypt = promise.promisifyAll(require('bcrypt'))
const SALT_WORK_FACTOR = 10

// create schema and model
const userSchema = new schema({

    email: {
      type: String,
      required: true,
      unique: true
    },
    password: {
      type: String,
      required: true
    }

})

userSchema.pre('save', function(next) {
    const user = this

    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next()

    // generate a salt
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
        if (err) return next(err)

        // hash the password using our new salt
        bcrypt.hash(user.password, salt, function(err, hash) {
            if (err) return next(err)

            // override the cleartext password with the hashed one
            user.password = hash
            next()
        })
    })
})

userSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    })
}


const user = mongoose.model('user', userSchema)

module.exports = user