-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
500a6e7
commit 7ca9b27
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var Author, crypto; | ||
crypto = require('crypto'); | ||
Author = function(options) { | ||
this.fill(options); | ||
this.db = options.db || Author.db; | ||
return this; | ||
}; | ||
Author.prototype.fill = function(options) { | ||
this.name = options.name; | ||
this.email = options.email; | ||
this.salt = options.salt || Author.generate_salt(); | ||
return (this.pw = options.pw); | ||
}; | ||
Author.prototype.set_password = function(pass) { | ||
return (this.pw = Author.hash_password(this.salt, pass)); | ||
}; | ||
Author.prototype.authenticates = function(pass) { | ||
return Author.hash_password(this.salt, pass) === this.pw; | ||
}; | ||
Author.generate_salt = function() { | ||
var hash; | ||
hash = crypto.createHash('md5'); | ||
hash.update(this.name); | ||
hash.update(this.email); | ||
hash.update(Math.random().toString()); | ||
return hash.digest('hex'); | ||
}; | ||
Author.hash_password = function(salt, pass) { | ||
var hash; | ||
hash = crypto.createHash('md5'); | ||
hash.update(salt); | ||
hash.update(pass); | ||
return hash.digest('hex'); | ||
}; | ||
exports.Author = Author; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
crypto = require('crypto') | ||
|
||
class Author | ||
constructor: (options) -> | ||
@fill options | ||
@db = options.db || Author.db | ||
|
||
fill: (options) -> | ||
@name = options.name | ||
@email = options.email | ||
@salt = options.salt || Author.generate_salt() | ||
@pw = options.pw # hashed password | ||
|
||
set_password: (pass) -> | ||
@pw = Author.hash_password @salt, pass | ||
|
||
authenticates: (pass) -> | ||
Author.hash_password(@salt, pass) == @pw | ||
|
||
Author.generate_salt = -> | ||
hash = crypto.createHash 'md5' | ||
hash.update @name | ||
hash.update @email | ||
hash.update Math.random().toString() # shoot me | ||
hash.digest 'hex' | ||
|
||
Author.hash_password = (salt, pass) -> | ||
hash = crypto.createHash 'md5' | ||
hash.update salt | ||
hash.update pass | ||
hash.digest 'hex' | ||
|
||
exports.Author = Author |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
assert = require 'assert' | ||
Author = require('../lib/author').Author | ||
|
||
author = new Author name: "Name", email: "Email" | ||
|
||
assert.equal true, author.salt? | ||
assert.equal false, author.pw? | ||
|
||
author.set_password "secret" | ||
|
||
assert.equal true, author.authenticates('secret') | ||
assert.equal false, author.authenticates('foo') |