passman/js/app/services/encryptservice.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2016-10-08 01:56:29 +08:00
(function () {
'use strict';
/**
* @ngdoc service
* @name passmanApp.EncryptService
* @description
* # EncryptService
* 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
};
2016-09-12 01:45:20 +08:00
2016-10-08 01:56:29 +08:00
return {
encryptString: function (string, _key) {
if (!_key) {
_key = VaultService.getActiveVault().vaultKey;
}
var rp = {};
var ct = sjcl.encrypt(_key, string, encryption_config, rp);
return window.btoa(ct);
},
decryptString: function (ciphertext, _key) {
if (!_key) {
_key = VaultService.getActiveVault().vaultKey;
}
ciphertext = window.atob(ciphertext);
var rp = {};
try {
return sjcl.decrypt(_key, ciphertext, encryption_config, rp);
} catch (e) {
throw e;
}
2016-09-12 01:45:20 +08:00
}
2016-09-29 04:05:09 +08:00
2016-10-08 01:56:29 +08:00
};
}]);
}());