Permalink
Browse files

Make encrypt/decrypt async triggered on ready and store in encrypted …

…instead of rawData.
  • Loading branch information...
1 parent 7b82030 commit a2897abba5e4b329c484101b00bd27c436604828 @Mardak Mardak committed Aug 27, 2011
Showing with 21 additions and 18 deletions.
  1. +10 −12 userData/crypto.js
  2. +11 −6 userData/rawData.js
View
@@ -81,21 +81,19 @@ function askPassword() {
}
// Encrypt data with the password hash
-exports.encrypt = function(data) {
- if (!ready)
- throw "not ready";
-
- var encrypter = crypto.createCipher("aes256", passwordHash);
- return encrypter.update(data) + encrypter.final();
+exports.encrypt = function(data, onEncrypt) {
+ exports.ready(function() {
+ var encrypter = crypto.createCipher("aes256", passwordHash);
+ onEncrypt(encrypter.update(data) + encrypter.final());
+ });
};
// Decrypt data with the password hash
-exports.decrypt = function(data) {
- if (!ready)
- throw "not ready";
-
- var decrypter = crypto.createDecipher("aes256", passwordHash);
- return decrypter.update(data) + decrypter.final();
+exports.decrypt = function(data, onDecrypt) {
+ exports.ready(function() {
+ var decrypter = crypto.createDecipher("aes256", passwordHash);
+ onDecrypt(decrypter.update(data) + decrypter.final());
+ });
};
// Remember if we're ready and what callbacks to trigger
View
@@ -1,6 +1,7 @@
+var crypto = require("./crypto");
var fs = require("fs");
-const DATA_PATH = "rawData";
+const DATA_PATH = "encrypted";
// Make sure the directory exists
fs.mkdir(DATA_PATH, 0750);
@@ -46,10 +47,12 @@ exports.readAll = function(onFile) {
// Grab the next file and update the remaining files
var file = DATA_PATH + "/" + files.shift();
- fs.readFile(file, "utf8", function(error, json) {
- // Give the callback a js object; grab more files when it returns true
- if (onFile(JSON.parse(json)))
- getNextFile();
+ fs.readFile(file, "utf8", function(error, blob) {
+ crypto.decrypt(blob, function(json) {
+ // Give the callback a js object; grab more files when it returns true
+ if (onFile(JSON.parse(json)))
+ getNextFile();
+ });
});
})();
});
@@ -76,7 +79,9 @@ exports.save = function(data) {
// Save the data to disk as JSON
var file = DATA_PATH + "/" + Date.now();
- fs.writeFile(file, JSON.stringify(data));
+ crypto.encrypt(JSON.stringify(data), function(blob) {
+ fs.writeFile(file, blob);
+ });
return true;
}

0 comments on commit a2897ab

Please sign in to comment.