2016-10-15 00:59:07 +02:00
|
|
|
var ethUtil = require('ethereumjs-util')
|
2016-10-13 05:03:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2016-10-15 01:26:44 +02:00
|
|
|
|
|
|
|
// Simple encryption methods:
|
2016-10-13 05:03:14 +02:00
|
|
|
encrypt,
|
|
|
|
decrypt,
|
2016-10-15 01:26:44 +02:00
|
|
|
|
|
|
|
// More advanced encryption methods:
|
2016-10-13 05:03:14 +02:00
|
|
|
keyFromPassword,
|
2016-10-14 22:21:00 +02:00
|
|
|
encryptWithKey,
|
|
|
|
decryptWithKey,
|
2016-10-15 01:26:44 +02:00
|
|
|
|
|
|
|
// Buffer <-> String methods
|
|
|
|
convertArrayBufferViewtoString,
|
|
|
|
convertStringToArrayBufferView,
|
|
|
|
|
|
|
|
// Buffer <-> Hex string methods
|
2016-10-15 00:59:07 +02:00
|
|
|
serializeBufferForStorage,
|
|
|
|
serializeBufferFromStorage,
|
2016-10-20 20:33:18 +02:00
|
|
|
|
|
|
|
// Buffer <-> base64 string methods
|
|
|
|
encodeBufferToBase64,
|
|
|
|
decodeBase64ToBuffer,
|
2016-10-21 21:11:54 +02:00
|
|
|
|
|
|
|
generateSalt,
|
2016-10-13 05:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a Pojo, returns encrypted text.
|
|
|
|
function encrypt (password, dataObj) {
|
2016-10-14 22:21:00 +02:00
|
|
|
return keyFromPassword(password)
|
|
|
|
.then(function (passwordDerivedKey) {
|
|
|
|
return encryptWithKey(passwordDerivedKey, dataObj)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function encryptWithKey (key, dataObj) {
|
2016-10-13 05:03:14 +02:00
|
|
|
var data = JSON.stringify(dataObj)
|
2016-10-14 22:21:00 +02:00
|
|
|
var dataBuffer = convertStringToArrayBufferView(data)
|
2016-10-15 01:26:44 +02:00
|
|
|
var vector = global.crypto.getRandomValues(new Uint8Array(16))
|
2016-10-14 22:21:00 +02:00
|
|
|
|
|
|
|
return global.crypto.subtle.encrypt({
|
|
|
|
name: 'AES-GCM',
|
2016-10-15 01:28:08 +02:00
|
|
|
iv: vector,
|
2016-10-15 00:59:07 +02:00
|
|
|
}, key, dataBuffer).then(function(buf){
|
|
|
|
var buffer = new Uint8Array(buf)
|
2016-10-20 21:07:53 +02:00
|
|
|
var vectorStr = encodeBufferToBase64(vector)
|
|
|
|
var vaultStr = encodeBufferToBase64(buffer)
|
|
|
|
return `${vaultStr}\\${vectorStr}`
|
2016-10-13 05:03:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes encrypted text, returns the restored Pojo.
|
|
|
|
function decrypt (password, text) {
|
2016-10-14 22:21:00 +02:00
|
|
|
return keyFromPassword(password)
|
|
|
|
.then(function (key) {
|
|
|
|
return decryptWithKey(key, text)
|
|
|
|
})
|
|
|
|
}
|
2016-10-13 05:03:14 +02:00
|
|
|
|
2016-10-14 22:21:00 +02:00
|
|
|
function decryptWithKey (key, text) {
|
2016-10-20 21:07:53 +02:00
|
|
|
const parts = text.split('\\')
|
|
|
|
const encryptedData = decodeBase64ToBuffer(parts[0])
|
|
|
|
const vector = decodeBase64ToBuffer(parts[1])
|
2016-10-15 01:28:08 +02:00
|
|
|
return crypto.subtle.decrypt({name: 'AES-GCM', iv: vector}, key, encryptedData)
|
2016-10-14 22:21:00 +02:00
|
|
|
.then(function(result){
|
|
|
|
const decryptedData = new Uint8Array(result)
|
2016-10-15 00:59:07 +02:00
|
|
|
const decryptedStr = convertArrayBufferViewtoString(decryptedData)
|
2016-10-14 22:21:00 +02:00
|
|
|
const decryptedObj = JSON.parse(decryptedStr)
|
|
|
|
return decryptedObj
|
|
|
|
})
|
2016-10-13 05:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function convertStringToArrayBufferView (str) {
|
|
|
|
var bytes = new Uint8Array(str.length)
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
|
|
|
bytes[i] = str.charCodeAt(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertArrayBufferViewtoString (buffer) {
|
|
|
|
var str = ''
|
|
|
|
for (var i = 0; i < buffer.byteLength; i++) {
|
|
|
|
str += String.fromCharCode(buffer[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
function keyFromPassword (password) {
|
2016-10-14 22:21:00 +02:00
|
|
|
var passBuffer = convertStringToArrayBufferView(password)
|
|
|
|
return global.crypto.subtle.digest('SHA-256', passBuffer)
|
|
|
|
.then(function (passHash){
|
|
|
|
return global.crypto.subtle.importKey('raw', passHash, {name: 'AES-GCM'}, false, ['encrypt', 'decrypt'])
|
2016-10-13 05:03:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-15 00:59:07 +02:00
|
|
|
function serializeBufferFromStorage (str) {
|
|
|
|
str = ethUtil.stripHexPrefix(str)
|
2016-10-15 01:28:08 +02:00
|
|
|
var buf = new Uint8Array(str.length / 2)
|
|
|
|
for (var i = 0; i < str.length; i += 2) {
|
2016-10-15 00:59:07 +02:00
|
|
|
var seg = str.substr(i, 2)
|
2016-10-15 01:28:08 +02:00
|
|
|
buf[i / 2] = parseInt(seg, 16)
|
2016-10-15 00:59:07 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2016-10-20 20:33:18 +02:00
|
|
|
|
|
|
|
function encodeBufferToBase64 (buf) {
|
|
|
|
var b64encoded = btoa(String.fromCharCode.apply(null, buf))
|
|
|
|
return b64encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeBase64ToBuffer (base64) {
|
2016-10-20 21:07:53 +02:00
|
|
|
var buf = new Uint8Array(atob(base64).split('')
|
2016-10-20 20:33:18 +02:00
|
|
|
.map(function(c) {
|
|
|
|
return c.charCodeAt(0)
|
|
|
|
}))
|
2016-10-20 21:07:53 +02:00
|
|
|
return buf
|
2016-10-20 20:33:18 +02:00
|
|
|
}
|
2016-10-21 21:11:54 +02:00
|
|
|
|
|
|
|
function generateSalt (byteCount = 32) {
|
|
|
|
var view = new Uint8Array(byteCount)
|
|
|
|
global.crypto.getRandomValues(view)
|
|
|
|
var b64encoded = btoa(String.fromCharCode.apply(null, view))
|
|
|
|
return b64encoded
|
|
|
|
}
|