Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 5022bc2

Browse files
committed
Use Scrypt for password hashes
1 parent 4825f44 commit 5022bc2

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

app/models/user.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var mongoose = require('mongoose'),
55
Schema = mongoose.Schema,
66
crypto = require('crypto'),
7+
scrypt = require('scrypt'),
78
_ = require('underscore'),
89
authTypes = ['github', 'twitter', 'facebook', 'google'];
910

@@ -17,7 +18,6 @@ var UserSchema = new Schema({
1718
username: String,
1819
provider: String,
1920
hashed_password: String,
20-
salt: String,
2121
facebook: {},
2222
twitter: {},
2323
github: {},
@@ -29,7 +29,6 @@ var UserSchema = new Schema({
2929
*/
3030
UserSchema.virtual('password').set(function(password) {
3131
this._password = password;
32-
this.salt = this.makeSalt();
3332
this.hashed_password = this.encryptPassword(password);
3433
}).get(function() {
3534
return this._password;
@@ -92,18 +91,9 @@ UserSchema.methods = {
9291
* @api public
9392
*/
9493
authenticate: function(plainText) {
95-
return this.encryptPassword(plainText) === this.hashed_password;
94+
return scrypt.verifyHashSync(this.hashed_password, plainText);
9695
},
9796

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-
},
10797

10898
/**
10999
* Encrypt password
@@ -114,8 +104,10 @@ UserSchema.methods = {
114104
*/
115105
encryptPassword: function(password) {
116106
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');
118110
}
119111
};
120112

121-
mongoose.model('User', UserSchema);
113+
mongoose.model('User', UserSchema);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"forever": "latest",
3737
"grunt": "latest",
3838
"grunt-cli": "latest",
39-
"grunt-bower-task": "latest"
39+
"grunt-bower-task": "latest",
40+
"scrypt": "latest"
4041
},
4142
"devDependencies": {
4243
"supertest": "latest",
@@ -47,4 +48,4 @@
4748
"grunt-concurrent": "latest",
4849
"grunt-mocha-test": "latest"
4950
}
50-
}
51+
}

0 commit comments

Comments
 (0)