From 9246ae175fcb34c3657a5a0b96cbb9503f0cff8f Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 18 Apr 2014 17:35:32 -0300 Subject: [PATCH] encryption and decryption working for File --- js/models/storage/File.js | 44 ++++++++++++++++++++++------- js/models/storage/LocalEncrypted.js | 2 +- test/test.storage.File.js | 13 +++++---- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/js/models/storage/File.js b/js/models/storage/File.js index 3fca9154a..50b44b6d9 100644 --- a/js/models/storage/File.js +++ b/js/models/storage/File.js @@ -2,7 +2,6 @@ var imports = require('soop').imports(); var fs = imports.fs || require('fs'); var parent = imports.parent || require('./Base'); -var crypto = imports.crypto || require('crypto'); var CryptoJS = require('node-cryptojs-aes').CryptoJS; var passwords = []; @@ -15,8 +14,33 @@ function Storage(opts) { } Storage.parent = parent; +Storage.prototype._encrypt = function(string) { + var encrypted = CryptoJS.AES.encrypt(string, passwords[0]); + var encryptedBase64 = encrypted.toString(); + return encryptedBase64; +}; + +Storage.prototype._encryptObj = function(obj) { + var string = JSON.stringify(obj); + return this._encrypt(string); +}; + +Storage.prototype._decrypt = function(base64) { + var decrypted = CryptoJS.AES.decrypt(base64, passwords[0]); + var decryptedStr = decrypted.toString(CryptoJS.enc.Utf8); + return decryptedStr; +}; + +Storage.prototype._decryptObj = function(base64) { + var decryptedStr = this._decrypt(base64); + return JSON.parse(decryptedStr); +}; + Storage.prototype.load = function(walletId, callback) { - fs.readFile(walletId, function(err, data) { + var self = this; + fs.readFile(walletId, function(err, base64) { + var data = self._decryptObj(base64); + if (err) return callback(err); try { @@ -32,10 +56,11 @@ Storage.prototype.load = function(walletId, callback) { }; Storage.prototype.save = function(walletId, callback) { - var data = JSON.stringify(this.data[walletId]); + var obj = this.data[walletId]; + var encryptedBase64 = this._encryptObj(obj); //TODO: update to use a queue to ensure that saves are made sequentially - fs.writeFile(walletId, data, function(err) { + fs.writeFile(walletId, encryptedBase64, function(err) { if (callback) return callback(err); }); @@ -105,16 +130,15 @@ Storage.prototype.setFromObj = function(walletId, obj, callback) { this.save(walletId, callback); }; -Storage.prototype.setFromEncryptedObj = function(walletId) { - //TODO: implement +Storage.prototype.setFromEncryptedObj = function(walletId, base64, callback) { + var obj = this._decryptObj(base64); + this.setFromObj(walletId, obj, callback); }; Storage.prototype.getEncryptedObj = function(walletId) { - var data = JSON.stringify(this.data[walletId]); - var encrypted = CryptoJS.AES.encrypt(data, passwords[0]); - var hex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(encrypted.toString())); + var encryptedBase64 = this._encryptObj(this.data[walletId]); - return hex; + return encryptedBase64; }; // remove all values diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index b730b7bce..cb4e378fe 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -54,7 +54,7 @@ Storage.prototype._write = function(k,v) { }; Storage.prototype.getEncryptedObj = function(walletId) { - var keys = this._getWalletKeys(); + var keys = this._getWalletKeys(walletId); var obj = {}; for (var i in keys) { var key = keys[0]; diff --git a/test/test.storage.File.js b/test/test.storage.File.js index 20da9cbba..97a33c9b0 100644 --- a/test/test.storage.File.js +++ b/test/test.storage.File.js @@ -17,7 +17,9 @@ describe('Storage/File', function() { var fs = {} fs.readFile = function(filename, callback) { filename.should.equal('myfilename'); - callback(); + var obj = {"test":"test"}; + var encryptedStr = CryptoJS.AES.encrypt(JSON.stringify(obj), "password").toString(); + callback(null, encryptedStr); }; var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs}); var storage = new Storage({password: 'password'}); @@ -156,15 +158,16 @@ describe('Storage/File', function() { var obj = {test:'testval'}; var data = JSON.stringify(obj); var encrypted = CryptoJS.AES.encrypt(data, 'password'); - var hex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(encrypted.toString())); + var base64 = encrypted.toString(); var storage = new Storage({password: 'password'}); storage.data['walletId'] = obj; var enc = storage.getEncryptedObj('walletId'); - enc.length.should.equal(96); - enc.slice(0,10).should.equal(hex.slice(0,10)); - enc.slice(0,6).should.equal("53616c"); + //enc.length.should.equal(96); + enc.length.should.be.greaterThan(10); + enc.slice(0,10).should.equal(base64.slice(0,10)); + //enc.slice(0,6).should.equal("53616c"); }); });