Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why am I getting two different encrypted hashes from node.bcrypt even though password and salt are the same?
- vows.describe('User').addBatch
- 'User':
- topic: ->
- user = new User
- name: 'test.user'
- '#authenticate':
- topic: (user) ->
- promise = new events.EventEmitter
- password = User.generatePassword()
- user.set 'password', password
- user.save (err) ->
- User.authenticate user.name, password, (err, user) ->
- if (err) promise.emit 'error', err
- else promise.emit 'success', user
- promise
- 'should return a user': (err, user) ->
- assert.isNotNull user
- bcrypt = require 'bcrypt'
- mongoose = require 'mongoose'
- User = new mongoose.Schema
- name: { type: String, unique: true }
- password_salt: String
- password_hash: String
- User.virtual('password')
- .set (pass) ->
- salt = bcrypt.gen_salt_sync(10)
- @set 'password_salt', salt
- @set 'password_hash', bcrypt.encrypt_sync pass, salt
- User.static
- generatePassword: ->
- # just generates 8 characters of alphanumerics
- authenticate: (name, password, next) ->
- @findOne { name: name }, (err, user) ->
- if user
- bcrypt.compare password, user.password_hash, (err, res) ->
- if res is true then next(err, user) else next(err, null)
- else
- next(err, user)
- # spec/models/user_spec.coffee
- # Rest of the test snippet above...
- user.save (err) ->
- console.log "1 Pass: #{password}n
- 1 Salt: #{user.password_salt}n
- 1 Hash: #{user.password_hash}"
- # Rest of the test snippet above...
- # models/user.coffee
- # Rest of user model snippet above...
- bcrypt.compare password, user.password_hash, (err, res) ->
- console.log "2 Pass: #{password}n
- 2 Salt: #{user.password_salt}n
- 2 Hash: #{user.password_hash}nn
- 2 Encrypt Again: #{bcrypt.encrypt_sync password, user.password_salt}"
- # Rest of user model snippet above...
- # console.log output
- 1 Pass: Q8ZE0Yj1
- 1 Salt: $2a$10$o0sRXPchaW2R841MWRCT3u
- 1 Hash: $2a$10$o0sRXPchaW2R841MWRCT3uj80ecPs1QX4kTfC/hzG.2kMa8RaCsZu
- 2 Pass: Q8ZE0Yj1
- 2 Salt: $2a$10$o0sRXPchaW2R841MWRCT3u
- 2 Hash: $2a$10$o0sRXPchaW2R841MWRCT3uj80ecPs1QX4kTfC/hzG.2kMa8RaCsZu
- 2 Encrypt Again: $2a$10$o0sRXPchaW2R841MWRCT3ukGkrfpJuzZFKJcdX0WiNHONuJTy
Add Comment
Please, Sign In to add comment