2016-09-12 01:45:20 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc service
|
2016-09-16 03:21:34 +08:00
|
|
|
* @name passmanApp.EncryptService
|
2016-09-12 01:45:20 +08:00
|
|
|
* @description
|
2016-09-16 03:21:34 +08:00
|
|
|
* # EncryptService
|
2016-09-12 01:45:20 +08:00
|
|
|
* Service in the passmanApp.
|
|
|
|
*/
|
|
|
|
angular.module('passmanApp')
|
|
|
|
.service('EncryptService', ['VaultService', function (VaultService) {
|
|
|
|
// AngularJS will instantiate a singleton by calling "new" on this function
|
|
|
|
var encryption_config = {
|
|
|
|
adata:"",
|
|
|
|
iter: 1000,
|
|
|
|
ks: 256,
|
|
|
|
mode: 'ccm',
|
|
|
|
ts:64
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2016-10-02 18:35:27 +08:00
|
|
|
encryptString: function(string, _key){
|
|
|
|
if(!_key) {
|
|
|
|
_key = VaultService.getActiveVault().vaultKey;
|
|
|
|
}
|
2016-09-12 02:47:29 +08:00
|
|
|
var rp = {};
|
2016-09-12 01:45:20 +08:00
|
|
|
var ct = sjcl.encrypt(_key, string, encryption_config, rp);
|
|
|
|
return window.btoa(ct);
|
|
|
|
},
|
2016-10-02 18:35:27 +08:00
|
|
|
decryptString: function(ciphertext, _key){
|
|
|
|
if(!_key) {
|
|
|
|
_key = VaultService.getActiveVault().vaultKey;
|
|
|
|
}
|
2016-09-12 01:45:20 +08:00
|
|
|
ciphertext = window.atob(ciphertext);
|
2016-09-12 02:47:29 +08:00
|
|
|
var rp = {};
|
2016-09-12 01:45:20 +08:00
|
|
|
try {
|
|
|
|
return sjcl.decrypt(_key, ciphertext, encryption_config, rp)
|
|
|
|
} catch(e) {
|
2016-09-28 04:02:04 +08:00
|
|
|
throw e;
|
2016-09-12 01:45:20 +08:00
|
|
|
}
|
2016-09-30 23:44:21 +08:00
|
|
|
}
|
2016-09-29 04:05:09 +08:00
|
|
|
|
2016-09-12 01:45:20 +08:00
|
|
|
}
|
|
|
|
}]);
|