aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2016-10-15 06:59:07 +0800
committerDan Finlay <dan@danfinlay.com>2016-10-15 06:59:07 +0800
commit1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c (patch)
treec285dbb78343a73d18ceb364cf9857420379ba2e
parentc9cfcd5253b29ef7240e556a847f525c38729fa0 (diff)
downloadtangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar.gz
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar.bz2
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar.lz
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar.xz
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.tar.zst
tangerine-wallet-browser-1c791c4d2e7f280b634a5fe1bb449d5d05c9db7c.zip
Got basic encryptor working
-rw-r--r--app/scripts/lib/encryptor.js44
-rw-r--r--test/integration/lib/encryptor-test.js31
2 files changed, 61 insertions, 14 deletions
diff --git a/app/scripts/lib/encryptor.js b/app/scripts/lib/encryptor.js
index 1ce95954f..207a9fc3c 100644
--- a/app/scripts/lib/encryptor.js
+++ b/app/scripts/lib/encryptor.js
@@ -1,3 +1,4 @@
+var ethUtil = require('ethereumjs-util')
var vector = global.crypto.getRandomValues(new Uint8Array(16))
module.exports = {
@@ -7,6 +8,8 @@ module.exports = {
keyFromPassword,
encryptWithKey,
decryptWithKey,
+ serializeBufferForStorage,
+ serializeBufferFromStorage,
}
// Takes a Pojo, returns encrypted text.
@@ -24,10 +27,9 @@ function encryptWithKey (key, dataObj) {
return global.crypto.subtle.encrypt({
name: 'AES-GCM',
iv: vector
- }, key, dataBuffer).then(function(result){
- const encryptedData = new Uint8Array(result)
- const encryptedStr = encryptedData.toString()
- return encryptedStr
+ }, key, dataBuffer).then(function(buf){
+ var buffer = new Uint8Array(buf)
+ return serializeBufferForStorage(buffer)
})
}
@@ -41,11 +43,12 @@ function decrypt (password, 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)
+ const encryptedData = serializeBufferFromStorage(text)
+ return crypto.subtle.decrypt({name: "AES-GCM", iv: vector}, key, encryptedData)
.then(function(result){
- debugger
const decryptedData = new Uint8Array(result)
- const decryptedStr = convertArrayBufferViewtoString(decryptedData))
+ const decryptedStr = convertArrayBufferViewtoString(decryptedData)
+ const numArr = decryptedStr.split(',')
const decryptedObj = JSON.parse(decryptedStr)
return decryptedObj
})
@@ -77,3 +80,30 @@ function keyFromPassword (password) {
})
}
+function serializeBufferFromStorage (str) {
+ str = ethUtil.stripHexPrefix(str)
+ var buf = new Uint8Array(str.length/2)
+ for (var i = 0; i < str.length; i+= 2) {
+ var seg = str.substr(i, 2)
+ buf[i/2] = parseInt(seg, 16)
+ }
+ return buf
+}
+
+// Should return a string, ready for storage, in hex format.
+function serializeBufferForStorage (buffer) {
+ var result = '0x'
+ var len = buffer.length || buffer.byteLength
+ for (var i = 0; i < len; i++) {
+ result += unprefixedHex(buffer[i])
+ }
+ return result
+}
+
+function unprefixedHex (num) {
+ var hex = num.toString(16)
+ while (hex.length < 2) {
+ hex = '0' + hex
+ }
+ return hex
+}
diff --git a/test/integration/lib/encryptor-test.js b/test/integration/lib/encryptor-test.js
index f5cdea835..3297ad177 100644
--- a/test/integration/lib/encryptor-test.js
+++ b/test/integration/lib/encryptor-test.js
@@ -1,6 +1,27 @@
var encryptor = require('../../../app/scripts/lib/encryptor')
-QUnit.test('encryptor', function(assert) {
+QUnit.test('encryptor:serializeBufferForStorage', function (assert) {
+ assert.expect(1)
+ var buf = new Buffer(2)
+ buf[0] = 16
+ buf[1] = 1
+
+ var output = encryptor.serializeBufferForStorage(buf)
+
+ var expect = '0x1001'
+ assert.equal(expect, output)
+})
+
+QUnit.test('encryptor:serializeBufferFromStorage', function (assert) {
+ assert.expect(2)
+ var input = '0x1001'
+ var output = encryptor.serializeBufferFromStorage(input)
+
+ assert.equal(output[0], 16)
+ assert.equal(output[1], 1)
+})
+
+QUnit.test('encryptor:encrypt & decrypt', function(assert) {
var done = assert.async();
var password, data, encrypted
@@ -9,19 +30,15 @@ QUnit.test('encryptor', function(assert) {
encryptor.encrypt(password, data)
.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')
+ assert.deepEqual(decryptedObj, data, 'decrypted what was encrypted')
done()
})
.catch(function(reason) {
+ debugger
assert.ifError(reason, 'threw an error')
})