diff options
author | Dan Finlay <dan@danfinlay.com> | 2016-10-15 04:21:00 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2016-10-15 04:21:00 +0800 |
commit | c9cfcd5253b29ef7240e556a847f525c38729fa0 (patch) | |
tree | 0be3d51950a3733b5a82db8e944b594bab4f8a4a | |
parent | cce8d9e3600e8ba0ced12013b0b208fff9f9c8a8 (diff) | |
download | tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar.gz tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar.bz2 tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar.lz tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar.xz tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.tar.zst tangerine-wallet-browser-c9cfcd5253b29ef7240e556a847f525c38729fa0.zip |
Got encrypting working, not yet decrypting
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | app/scripts/lib/encryptor.js | 44 | ||||
-rw-r--r-- | test/integration/bundle.js | 103 | ||||
-rw-r--r-- | test/integration/lib/encryptor-test.js | 15 | ||||
-rw-r--r-- | testem.yml | 1 |
5 files changed, 51 insertions, 115 deletions
diff --git a/.gitignore b/.gitignore index fa8a9151f..0b649d486 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ dist - node_modules temp .tmp @@ -7,10 +6,10 @@ temp app/bower_components test/bower_components package - .DS_Store builds/ notes.txt app/.DS_Store development/bundle.js builds.zip +test/integration/bundle.js diff --git a/app/scripts/lib/encryptor.js b/app/scripts/lib/encryptor.js index 607825764..1ce95954f 100644 --- a/app/scripts/lib/encryptor.js +++ b/app/scripts/lib/encryptor.js @@ -1,28 +1,54 @@ var vector = global.crypto.getRandomValues(new Uint8Array(16)) -var key = null module.exports = { encrypt, decrypt, convertArrayBufferViewtoString, keyFromPassword, + encryptWithKey, + decryptWithKey, } // Takes a Pojo, returns encrypted text. function encrypt (password, dataObj) { + return keyFromPassword(password) + .then(function (passwordDerivedKey) { + return encryptWithKey(passwordDerivedKey, dataObj) + }) +} + +function encryptWithKey (key, dataObj) { var data = JSON.stringify(dataObj) - global.crypto.subtle.encrypt({name: 'AES-CBC', iv: vector}, key, convertStringToArrayBufferView(data)).then(function(result){ + var dataBuffer = convertStringToArrayBufferView(data) + + return global.crypto.subtle.encrypt({ + name: 'AES-GCM', + iv: vector + }, key, dataBuffer).then(function(result){ const encryptedData = new Uint8Array(result) - return encryptedData - }, - function(e){ - console.log(e.message) + const encryptedStr = encryptedData.toString() + return encryptedStr }) } // Takes encrypted text, returns the restored Pojo. function decrypt (password, text) { + return keyFromPassword(password) + .then(function (key) { + return decryptWithKey(key, text) + }) +} +// AUDIT: See if this still works when generating a fresh vector +function decryptWithKey (key, text) { + return crypto.subtle.decrypt({name: "AES-CBC", iv: vector}, key, encrypted_data) + .then(function(result){ + debugger + const decryptedData = new Uint8Array(result) + const decryptedStr = convertArrayBufferViewtoString(decryptedData)) + const decryptedObj = JSON.parse(decryptedStr) + return decryptedObj + }) } function convertStringToArrayBufferView (str) { @@ -44,8 +70,10 @@ function convertArrayBufferViewtoString (buffer) { } function keyFromPassword (password) { - global.crypto.subtle.digest({name: 'SHA-256'}, convertStringToArrayBufferView(password)).then(function(result){ - return global.crypto.subtle.importKey('raw', result, {name: 'AES-CBC'}, false, ['encrypt', 'decrypt']) + var passBuffer = convertStringToArrayBufferView(password) + return global.crypto.subtle.digest('SHA-256', passBuffer) + .then(function (passHash){ + return global.crypto.subtle.importKey('raw', passHash, {name: 'AES-GCM'}, false, ['encrypt', 'decrypt']) }) } diff --git a/test/integration/bundle.js b/test/integration/bundle.js deleted file mode 100644 index 5058259b1..000000000 --- a/test/integration/bundle.js +++ /dev/null @@ -1,103 +0,0 @@ -window.QUnit = QUnit; (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ -(function (global){ -"use strict"; - -var vector = global.crypto.getRandomValues(new Uint8Array(16)); - -module.exports = { - encrypt: encrypt, - decrypt: decrypt -}; - -// Takes a Pojo, returns encrypted text. -function encrypt(password, dataObj) { - var data = JSON.stringify(dataObj); - global.crypto.subtle.encrypt({ name: "AES-CBC", iv: vector }, key, convertStringToArrayBufferView(data)).then(function (result) { - var encryptedData = new Uint8Array(result); - return encryptedData; - }, function (e) { - console.log(e.message); - }); -} - -// Takes encrypted text, returns the restored Pojo. -function decrypt(password, text) {} - -function convertStringToArrayBufferView(str) { - var bytes = new Uint8Array(str.length); - for (var i = 0; i < str.length; i++) { - bytes[i] = str.charCodeAt(i); - } - - return bytes; -} - -function convertArrayBufferViewtoString(buffer) { - var str = ""; - for (var i = 0; i < buffer.byteLength; i++) { - str += String.fromCharCode(buffer[i]); - } - - return str; -} - -var password = "password"; - -var key = null; - -function keyFromPassword(password) { - global.crypto.subtle.digest({ name: "SHA-256" }, convertStringToArrayBufferView(password)).then(function (result) { - return global.crypto.subtle.importKey("raw", result, { name: "AES-CBC" }, false, ["encrypt", "decrypt"]); - }); -} - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{}],2:[function(require,module,exports){ -'use strict'; - -var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; - -var encryptor = require('../../../app/scripts/lib/encryptor'); - -QUnit.test('encryptor', function (assert) { - var password, data, encrypted; - - password = 'a sample passw0rd'; - data = { foo: 'data to encrypt' }; - - encryptor.encrypt(password, data).then(function (result) { - assert.equal(typeof result === 'undefined' ? 'undefined' : _typeof(result), 'string', 'returns a string'); - }).catch(function (reason) { - assert.ifError(reason, 'threw an error'); - }); -}); - -},{"../../../app/scripts/lib/encryptor":1}],3:[function(require,module,exports){ -'use strict'; - -QUnit.test('agree to terms', function (assert) { - var done = assert.async(); - - // Select the mock app root - var app = $('iframe').contents().find('#app-content .mock-app-root'); - - app.find('.markdown').prop('scrollTop', 100000000); - - wait().then(function () { - app.find('button').click(); - }).then(function () { - return wait(); - }).then(function () { - var title = app.find('h1').text(); - assert.equal(title, 'MetaMask', 'title screen'); - - var buttons = app.find('button'); - assert.equal(buttons.length, 1, 'one button: create new vault'); - - done(); - }); - - // Wait for view to transition: -}); - -},{}]},{},[2,3]); diff --git a/test/integration/lib/encryptor-test.js b/test/integration/lib/encryptor-test.js index 21d6ee6f7..f5cdea835 100644 --- a/test/integration/lib/encryptor-test.js +++ b/test/integration/lib/encryptor-test.js @@ -1,14 +1,25 @@ var encryptor = require('../../../app/scripts/lib/encryptor') QUnit.test('encryptor', function(assert) { + var done = assert.async(); var password, data, encrypted password = 'a sample passw0rd' data = { foo: 'data to encrypt' } encryptor.encrypt(password, data) - .then(function(result) { - assert.equal(typeof result, 'string', 'returns a string') + .then(function(encryptedStr) { + + assert.equal(typeof encryptedStr, 'string', 'returns a string') + + // Now try decrypting!jk + // + return encryptor.decrypt(password, encryptedStr) + + }) + .then(function (decryptedObj) { + assert.equal(decryptedObj, data, 'decrypted what was encrypted') + done() }) .catch(function(reason) { assert.ifError(reason, 'threw an error') diff --git a/testem.yml b/testem.yml index 7923a2929..2cf40f7f4 100644 --- a/testem.yml +++ b/testem.yml @@ -6,4 +6,5 @@ launch_in_ci: - Firefox framework: - qunit +before_tests: "npm run buildCiUnits" test_page: "test/integration/index.html" |