Removed oboslete code from encryptionService and gave it a better layout.

This commit is contained in:
Brendon Duncan 2018-06-28 20:49:54 +12:00
commit 63ddf545e4

View file

@ -1,15 +1,18 @@
(function() {
'use strict'; 'use strict';
angular.module('copayApp.services').factory('encryptionService', function($log, secureStorageService) {
var root = {};
angular.module('copayApp.services').factory('encryptionService', function($log, secureStorageService) {
var keySize = 512; var keySize = 512;
var iterations = 1500; var iterations = 1500;
var storageKey = 'encryptionKey'; var storageKey = 'encryptionKey';
// need a function to get the key var service = {
var password = 'password'; decrypt: decrypt,
encrypt: encrypt
};
return service;
function _generateKey() { function _generateKey() {
var salt = CryptoJS.lib.WordArray.random(128/8); var salt = CryptoJS.lib.WordArray.random(128/8);
@ -25,7 +28,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
* @param {*} cb * @param {*} cb
*/ */
function _getOrCreateKey(cb) { function _getOrCreateKey(cb) {
// TODO: Get from secure storage
secureStorageService.get(storageKey, function onKeyRetrieved(keyErr, keyHex) { secureStorageService.get(storageKey, function onKeyRetrieved(keyErr, keyHex) {
if (keyErr) { if (keyErr) {
cb(keyErr, null); cb(keyErr, null);
@ -90,7 +93,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
$log.debug('iv: ' + iv); $log.debug('iv: ' + iv);
// Just for testing - do we get back what we put in? // Just for testing - do we get back what we put in?
root.decrypt(ciphertext, {iv: iv}, function onDecryptionTest(err, decrypted){ decrypt(ciphertext, {iv: iv}, function onDecryptionTest(err, decrypted){
if (err) { if (err) {
$log.error('Failed to decrypt encrypted.', err); $log.error('Failed to decrypt encrypted.', err);
@ -110,7 +113,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
}; };
} }
root.decrypt = function(str, opts, cb) { function decrypt(str, opts, cb) {
_getOrCreateKey(function onKey(err, key) { _getOrCreateKey(function onKey(err, key) {
if (err) { if (err) {
$log.error('Failed to get or create key.', err); $log.error('Failed to get or create key.', err);
@ -123,7 +126,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
}); });
}; };
root.encrypt = function(str, cb) { function encrypt(str, cb) {
$log.debug('encrypt()', JSON.stringify('str')); $log.debug('encrypt()', JSON.stringify('str'));
$log.debug('*** crypto exists: ' + !!crypto); $log.debug('*** crypto exists: ' + !!crypto);
$log.debug('*** CryptoJS exists: ' + !!CryptoJS); $log.debug('*** CryptoJS exists: ' + !!CryptoJS);
@ -139,67 +142,5 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
}); });
}; };
root.encryptedObjectFromString = function(str) {
try {
var parsed = JSON.parse(str);
} catch(e) {
return null;
}
if (parsed.encryptionVersion) {
return parsed;
} else {
return null;
}
};
var JsonFormatter = {
stringify: function (cipherParams) {
// create json object with ciphertext
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
// optionally add iv and salt
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
// stringify json object
return JSON.stringify(jsonObj);
},
parse: function (jsonStr) {
// parse json string
var jsonObj = JSON.parse(jsonStr);
// extract ciphertext from json object, and create cipher params object
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
// optionally extract iv and salt
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
}
return cipherParams;
}
};
/*
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", { format: JsonFormatter });
alert(encrypted); // {"ct":"tZ4MsEnfbcDOwqau68aOrQ==","iv":"8a8c8fd8fe33743d3638737ea4a00698","s":"ba06373c8f57179c"}
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase", { format: JsonFormatter });
alert(decrypted.toString(CryptoJS.enc.Utf8)); // Message
*/
return root;
}); });
})();