Fixed vector usage in encryptor
This commit is contained in:
parent
1c791c4d2e
commit
69aed23e9b
|
@ -2,12 +2,21 @@ 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 = {
|
||||||
|
|
||||||
|
// Simple encryption methods:
|
||||||
encrypt,
|
encrypt,
|
||||||
decrypt,
|
decrypt,
|
||||||
convertArrayBufferViewtoString,
|
|
||||||
|
// More advanced encryption methods:
|
||||||
keyFromPassword,
|
keyFromPassword,
|
||||||
encryptWithKey,
|
encryptWithKey,
|
||||||
decryptWithKey,
|
decryptWithKey,
|
||||||
|
|
||||||
|
// Buffer <-> String methods
|
||||||
|
convertArrayBufferViewtoString,
|
||||||
|
convertStringToArrayBufferView,
|
||||||
|
|
||||||
|
// Buffer <-> Hex string methods
|
||||||
serializeBufferForStorage,
|
serializeBufferForStorage,
|
||||||
serializeBufferFromStorage,
|
serializeBufferFromStorage,
|
||||||
}
|
}
|
||||||
|
@ -23,13 +32,15 @@ function encrypt (password, dataObj) {
|
||||||
function encryptWithKey (key, dataObj) {
|
function encryptWithKey (key, dataObj) {
|
||||||
var data = JSON.stringify(dataObj)
|
var data = JSON.stringify(dataObj)
|
||||||
var dataBuffer = convertStringToArrayBufferView(data)
|
var dataBuffer = convertStringToArrayBufferView(data)
|
||||||
|
var vector = global.crypto.getRandomValues(new Uint8Array(16))
|
||||||
|
|
||||||
return global.crypto.subtle.encrypt({
|
return global.crypto.subtle.encrypt({
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv: vector
|
iv: vector
|
||||||
}, key, dataBuffer).then(function(buf){
|
}, key, dataBuffer).then(function(buf){
|
||||||
var buffer = new Uint8Array(buf)
|
var buffer = new Uint8Array(buf)
|
||||||
return serializeBufferForStorage(buffer)
|
var vectorStr = serializeBufferForStorage(vector)
|
||||||
|
return serializeBufferForStorage(buffer) + vectorStr
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +54,10 @@ 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) {
|
||||||
const encryptedData = serializeBufferFromStorage(text)
|
const parts = text.split('0x')
|
||||||
|
const encryptedData = serializeBufferFromStorage(parts[1])
|
||||||
|
const vector = serializeBufferFromStorage(parts[2])
|
||||||
|
debugger
|
||||||
return crypto.subtle.decrypt({name: "AES-GCM", iv: vector}, key, encryptedData)
|
return crypto.subtle.decrypt({name: "AES-GCM", iv: vector}, key, encryptedData)
|
||||||
.then(function(result){
|
.then(function(result){
|
||||||
const decryptedData = new Uint8Array(result)
|
const decryptedData = new Uint8Array(result)
|
||||||
|
|
Loading…
Reference in New Issue