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