passman/js/app/services/encryptservice.js

41 lines
948 B
JavaScript
Raw Normal View History

2016-09-12 01:45:20 +08:00
'use strict';
/**
* @ngdoc service
* @name passmanApp.VaultService
* @description
* # VaultService
* 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 {
encryptString: function(string){
var _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);
},
decryptString: function(ciphertext){
ciphertext = window.atob(ciphertext);
var _key = VaultService.getActiveVault().vaultKey;
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) {
Error("Can't decrypt: "+e);
return;
}
}
}
}]);