Created
July 14, 2013 06:26
-
-
Save ieve-rothe/5993384 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //db.js | |
| createUserContainer: function(username, password, callback) { | |
| if (username && password) { | |
| var raw_message = 'BLG' + username; | |
| var fileString = __dirname + '/../user_data/' + username + '.blg'; | |
| applib.hashMessage(raw_message, password, function(message) { | |
| fs.writeFile(fileString, message, function(err) { | |
| callback(err); | |
| }) | |
| }) | |
| } | |
| } | |
| verifyUserContainer: function(username, password, callback) { | |
| var container_path = __dirname + '/../user_data/' + username + '.blg'; | |
| if (username && password) { | |
| fs.exists(container_path, function(exists) { | |
| if (exists) { | |
| var expected_message = 'BLG' + username; | |
| module.exports.readLine(container_path, 'utf8', 1, function(line) { | |
| applib.decryptMessage(line.toString(), password, function(message) { | |
| if (message == expected_message) { | |
| callback(true) | |
| } | |
| else { | |
| callback(false); | |
| } | |
| }) | |
| }) | |
| } | |
| else { | |
| callback(false); | |
| } | |
| }) | |
| } | |
| } | |
| // applib.js | |
| hashMessage: function(message, password, callback) { | |
| var hash_algorithm = 'aes256'; | |
| var cipher = crypto.createCipher(hash_algorithm, password); | |
| var encrypted = cipher.update(message, 'utf8', 'hex') + cipher.final('hex'); | |
| callback(encrypted); | |
| } | |
| decryptMessage: function(ciphertext, password, callback) { | |
| var hash_algorithm = 'aes256'; | |
| var decipher = crypto.createDecipher(hash_algorithm, password); | |
| var decrypted = decipher.update(ciphertext, 'hex','utf8') + decipher.final('utf8'); | |
| callback(decrypted); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment