From 74129a69235e7c49483034272d343939c80be0d8 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 30 Sep 2014 20:29:56 -0300 Subject: [PATCH] mv Passphrase logic to Storage --- js/models/Identity.js | 1 - js/models/Storage.js | 20 ++++++++++++++++---- test/Storage.js | 6 +++--- test/mocks/FakeLocalStorage.js | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index c7e5f9a05..d026c3ea1 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -113,7 +113,6 @@ Identity.create = function(email, password, opts, cb) { Identity._createProfile(email, password, iden.storage, function(err, profile) { if (err) return cb(err); iden.profile = profile; -console.log('[Identity.js.115:profile:]',profile); //TODO if (opts.noWallets) cb(null, iden); diff --git a/js/models/Storage.js b/js/models/Storage.js index 9880d31d0..c74211d51 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -2,6 +2,7 @@ var preconditions = require('preconditions').singleton(); var CryptoJS = require('node-cryptojs-aes').CryptoJS; var bitcore = require('bitcore'); +var Passphrase = require('./Passphrase'); var preconditions = require('preconditions').instance(); var _ = require('underscore'); var CACHE_DURATION = 1000 * 60 * 5; @@ -17,11 +18,15 @@ var id = 0; function Storage(opts) { preconditions.checkArgument(opts); preconditions.checkArgument(opts.password); - opts = opts || {}; this.wListCache = {}; this.__uniqueid = ++id; - this.setPassphrase(opts.password); + this.passphraseConfig = { + salt: opts.salt, + iterations: opts.iterations, + }; + + this.setPassword(opts.password); try { this.db = opts.db || localStorage; @@ -48,8 +53,15 @@ Storage.prototype.hasPassphrase = function() { return pps[this.__uniqueid] ? true : false; }; -Storage.prototype.setPassphrase = function(password) { - pps[this.__uniqueid] = password; + +Storage.prototype._setPassphrase = function(passphrase) { + pps[this.__uniqueid] = passphrase; +}; + +Storage.prototype.setPassword = function(password, config) { + var passphraseConfig = _.extend(this.passphraseConfig, config); + var p = new Passphrase(passphraseConfig); + this._setPassphrase(p.getBase64(password)); } Storage.prototype._encrypt = function(string) { diff --git a/test/Storage.js b/test/Storage.js index 74b95fb10..842169528 100644 --- a/test/Storage.js +++ b/test/Storage.js @@ -9,7 +9,7 @@ describe('Storage model', function() { var s; beforeEach(function(done) { s = new Storage(requireMock('FakeLocalStorage').storageParams); - s.setPassphrase('mysupercoolpassword'); + s.setPassword('mysupercoolpassword'); s.clearAll(done); }); @@ -415,13 +415,13 @@ describe('Storage model', function() { describe('#decrypt', function() { it('should not be able to decrypt with wrong password', function() { - s.setPassphrase('xxx'); + s.setPassword('xxx'); var wo = s.decrypt(encryptedLegacy1); should.not.exist(wo); }); it('should be able to decrypt an old backup', function() { - s.setPassphrase(legacyPassword1); + s._setPassphrase(legacyPassword1); var wo = s.decrypt(encryptedLegacy1); should.exist(wo); wo.opts.id.should.equal('48ba2f1ffdfe9708'); diff --git a/test/mocks/FakeLocalStorage.js b/test/mocks/FakeLocalStorage.js index 56a6b5aab..6fad99249 100644 --- a/test/mocks/FakeLocalStorage.js +++ b/test/mocks/FakeLocalStorage.js @@ -32,4 +32,5 @@ module.exports.storageParams = { password: '123', db: new FakeLocalStorage(), sessionStorage: new FakeLocalStorage(), + iterations: 1, };