1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Got basic encryptor working

This commit is contained in:
Dan Finlay 2016-10-14 15:59:07 -07:00
parent c9cfcd5253
commit 1c791c4d2e
2 changed files with 61 additions and 14 deletions

View File

@ -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
}

View File

@ -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')
})