Skip to content

Instantly share code, notes, and snippets.

@ieve-rothe
Created July 14, 2013 06:26
Show Gist options
  • Select an option

  • Save ieve-rothe/5993384 to your computer and use it in GitHub Desktop.

Select an option

Save ieve-rothe/5993384 to your computer and use it in GitHub Desktop.
//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