mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Got basic encryptor working
This commit is contained in:
parent
c9cfcd5253
commit
1c791c4d2e
@ -1,3 +1,4 @@
|
|||||||
|
var ethUtil = require('ethereumjs-util')
|
||||||
var vector = global.crypto.getRandomValues(new Uint8Array(16))
|
var vector = global.crypto.getRandomValues(new Uint8Array(16))
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -7,6 +8,8 @@ module.exports = {
|
|||||||
keyFromPassword,
|
keyFromPassword,
|
||||||
encryptWithKey,
|
encryptWithKey,
|
||||||
decryptWithKey,
|
decryptWithKey,
|
||||||
|
serializeBufferForStorage,
|
||||||
|
serializeBufferFromStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a Pojo, returns encrypted text.
|
// Takes a Pojo, returns encrypted text.
|
||||||
@ -24,10 +27,9 @@ function encryptWithKey (key, dataObj) {
|
|||||||
return global.crypto.subtle.encrypt({
|
return global.crypto.subtle.encrypt({
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv: vector
|
iv: vector
|
||||||
}, key, dataBuffer).then(function(result){
|
}, key, dataBuffer).then(function(buf){
|
||||||
const encryptedData = new Uint8Array(result)
|
var buffer = new Uint8Array(buf)
|
||||||
const encryptedStr = encryptedData.toString()
|
return serializeBufferForStorage(buffer)
|
||||||
return encryptedStr
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,11 +43,12 @@ function decrypt (password, text) {
|
|||||||
|
|
||||||
// AUDIT: See if this still works when generating a fresh vector
|
// AUDIT: See if this still works when generating a fresh vector
|
||||||
function decryptWithKey (key, text) {
|
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){
|
.then(function(result){
|
||||||
debugger
|
|
||||||
const decryptedData = new Uint8Array(result)
|
const decryptedData = new Uint8Array(result)
|
||||||
const decryptedStr = convertArrayBufferViewtoString(decryptedData))
|
const decryptedStr = convertArrayBufferViewtoString(decryptedData)
|
||||||
|
const numArr = decryptedStr.split(',')
|
||||||
const decryptedObj = JSON.parse(decryptedStr)
|
const decryptedObj = JSON.parse(decryptedStr)
|
||||||
return decryptedObj
|
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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,27 @@
|
|||||||
var encryptor = require('../../../app/scripts/lib/encryptor')
|
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 done = assert.async();
|
||||||
var password, data, encrypted
|
var password, data, encrypted
|
||||||
|
|
||||||
@ -9,19 +30,15 @@ QUnit.test('encryptor', function(assert) {
|
|||||||
|
|
||||||
encryptor.encrypt(password, data)
|
encryptor.encrypt(password, data)
|
||||||
.then(function(encryptedStr) {
|
.then(function(encryptedStr) {
|
||||||
|
|
||||||
assert.equal(typeof encryptedStr, 'string', 'returns a string')
|
assert.equal(typeof encryptedStr, 'string', 'returns a string')
|
||||||
|
|
||||||
// Now try decrypting!jk
|
|
||||||
//
|
|
||||||
return encryptor.decrypt(password, encryptedStr)
|
return encryptor.decrypt(password, encryptedStr)
|
||||||
|
|
||||||
})
|
})
|
||||||
.then(function (decryptedObj) {
|
.then(function (decryptedObj) {
|
||||||
assert.equal(decryptedObj, data, 'decrypted what was encrypted')
|
assert.deepEqual(decryptedObj, data, 'decrypted what was encrypted')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
.catch(function(reason) {
|
.catch(function(reason) {
|
||||||
|
debugger
|
||||||
assert.ifError(reason, 'threw an error')
|
assert.ifError(reason, 'threw an error')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user