4
4
var mongoose = require ( 'mongoose' ) ,
5
5
Schema = mongoose . Schema ,
6
6
crypto = require ( 'crypto' ) ,
7
+ scrypt = require ( 'scrypt' ) ,
7
8
_ = require ( 'underscore' ) ,
8
9
authTypes = [ 'github' , 'twitter' , 'facebook' , 'google' ] ;
9
10
@@ -17,7 +18,6 @@ var UserSchema = new Schema({
17
18
username : String ,
18
19
provider : String ,
19
20
hashed_password : String ,
20
- salt : String ,
21
21
facebook : { } ,
22
22
twitter : { } ,
23
23
github : { } ,
@@ -29,7 +29,6 @@ var UserSchema = new Schema({
29
29
*/
30
30
UserSchema . virtual ( 'password' ) . set ( function ( password ) {
31
31
this . _password = password ;
32
- this . salt = this . makeSalt ( ) ;
33
32
this . hashed_password = this . encryptPassword ( password ) ;
34
33
} ) . get ( function ( ) {
35
34
return this . _password ;
@@ -92,18 +91,9 @@ UserSchema.methods = {
92
91
* @api public
93
92
*/
94
93
authenticate : function ( plainText ) {
95
- return this . encryptPassword ( plainText ) === this . hashed_password ;
94
+ return scrypt . verifyHashSync ( this . hashed_password , plainText ) ;
96
95
} ,
97
96
98
- /**
99
- * Make salt
100
- *
101
- * @return {String }
102
- * @api public
103
- */
104
- makeSalt : function ( ) {
105
- return Math . round ( ( new Date ( ) . valueOf ( ) * Math . random ( ) ) ) + '' ;
106
- } ,
107
97
108
98
/**
109
99
* Encrypt password
@@ -114,8 +104,10 @@ UserSchema.methods = {
114
104
*/
115
105
encryptPassword : function ( password ) {
116
106
if ( ! password ) return '' ;
117
- return crypto . createHmac ( 'sha1' , this . salt ) . update ( password ) . digest ( 'hex' ) ;
107
+ var maxtime = 0.1 ;
108
+ return scrypt . passwordHashSync ( password , maxtime ) ;
109
+ //return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
118
110
}
119
111
} ;
120
112
121
- mongoose . model ( 'User' , UserSchema ) ;
113
+ mongoose . model ( 'User' , UserSchema ) ;
0 commit comments