Removed oboslete code from encryptionService and gave it a better layout.
This commit is contained in:
parent
f04417bc39
commit
63ddf545e4
1 changed files with 134 additions and 193 deletions
|
|
@ -1,15 +1,18 @@
|
|||
|
||||
(function() {
|
||||
'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 iterations = 1500;
|
||||
var storageKey = 'encryptionKey';
|
||||
|
||||
// need a function to get the key
|
||||
var password = 'password';
|
||||
var service = {
|
||||
decrypt: decrypt,
|
||||
encrypt: encrypt
|
||||
};
|
||||
return service;
|
||||
|
||||
function _generateKey() {
|
||||
var salt = CryptoJS.lib.WordArray.random(128/8);
|
||||
|
|
@ -25,7 +28,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
|
|||
* @param {*} cb
|
||||
*/
|
||||
function _getOrCreateKey(cb) {
|
||||
// TODO: Get from secure storage
|
||||
|
||||
secureStorageService.get(storageKey, function onKeyRetrieved(keyErr, keyHex) {
|
||||
if (keyErr) {
|
||||
cb(keyErr, null);
|
||||
|
|
@ -90,7 +93,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
|
|||
$log.debug('iv: ' + iv);
|
||||
|
||||
// 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) {
|
||||
$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) {
|
||||
if (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('*** crypto exists: ' + !!crypto);
|
||||
$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;
|
||||
});
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue