mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
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))
|
||||
|
||||
module.exports = {
|
||||
|
||||
// Simple encryption methods:
|
||||
encrypt,
|
||||
decrypt,
|
||||
convertArrayBufferViewtoString,
|
||||
|
||||
// More advanced encryption methods:
|
||||
keyFromPassword,
|
||||
encryptWithKey,
|
||||
decryptWithKey,
|
||||
|
||||
// Buffer <-> String methods
|
||||
convertArrayBufferViewtoString,
|
||||
convertStringToArrayBufferView,
|
||||
|
||||
// Buffer <-> Hex string methods
|
||||
serializeBufferForStorage,
|
||||
serializeBufferFromStorage,
|
||||
}
|
||||
@ -23,13 +32,15 @@ function encrypt (password, dataObj) {
|
||||
function encryptWithKey (key, dataObj) {
|
||||
var data = JSON.stringify(dataObj)
|
||||
var dataBuffer = convertStringToArrayBufferView(data)
|
||||
var vector = global.crypto.getRandomValues(new Uint8Array(16))
|
||||
|
||||
return global.crypto.subtle.encrypt({
|
||||
name: 'AES-GCM',
|
||||
iv: vector
|
||||
}, key, dataBuffer).then(function(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
|
||||
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)
|
||||
.then(function(result){
|
||||
const decryptedData = new Uint8Array(result)
|
||||
|
Loading…
Reference in New Issue
Block a user