UNPKG

crypto-browser-storage

Version:

☢ A simple package for secure local storage data by encryption using Crypto.JS

164 lines (158 loc) 4.95 kB
import * as i0 from '@angular/core'; import { InjectionToken, Injectable, Inject } from '@angular/core'; import * as CryptoJS from 'crypto-js'; import * as Generator from 'generate-js'; // @ts-ignore const SecureStorage = Generator.generate(function SecureStorage(storage, options) { //@ts-ignore var _ = this; _.storage = storage; if (options) { _.hash = options.hash; _.encrypt = options.encrypt; _.decrypt = options.decrypt; } }); function through(data) { return data; } SecureStorage.definePrototype({ hash: through, encrypt: through, decrypt: through, }, { writable: true }); SecureStorage.definePrototype({ getItem: function getItem(key) { var _ = this; key = _.hash(key); var value = _.storage.getItem(key); if (typeof value !== 'string') { return value; } value = _.decrypt(value); return JSON.parse(value); }, setItem: function setItem(key, value) { var _ = this; key = _.hash(key); value = JSON.stringify(value); value = _.encrypt(value); return _.storage.setItem(key, value); }, removeItem: function removeItem(key) { var _ = this; key = _.hash(key); return _.storage.removeItem(key); }, clear: function clear() { var _ = this; return _.storage.clear(); }, key: function key(id) { var _ = this; return _.storage.key(id); }, length: { get: function getLength() { var _ = this; // @ts-ignore return _.storage.length; } } }); const CRYPTO_HASH_KEY = new InjectionToken('CRYPTO_HASH_KEY'); class CryptoBrowserStorageService { secureStorage; constructor(cryptoHashKey) { this.secureStorage = new SecureStorage(localStorage, { hash: function hash(key) { //@ts-ignore key = CryptoJS.SHA256(key, cryptoHashKey); return key.toString(); }, encrypt: function encrypt(data) { data = CryptoJS.AES.encrypt(data, cryptoHashKey); data = data.toString(); return data; }, decrypt: function decrypt(data) { data = CryptoJS.AES.decrypt(data, cryptoHashKey); data = data.toString(CryptoJS.enc.Utf8); return data; }, }); } /** * Set data to localstorage via key * @param {any} key:string * @param {any} data: any * @returns {void} void */ setCache(key, data) { try { if (data) { this.secureStorage.setItem(key, data); } else { console.log(`Something went wrong to set ${key} cached`); } } catch (error) { console.log(`Error : "${key}" Set Cache:`, error); } } /** * Get localstorage data via key * @param {any} key:string * @returns {any} any: number | string | object | array */ getCache(key) { try { return this.secureStorage.getItem(key); } catch (error) { console.log('Key not found in localstorage or maybe wrong key.'); } } /** * It will remove stored local storage data via key * @param {any} key:string * @returns {void} void */ removeCacheByKey(key) { try { this.secureStorage.removeItem(key); } catch (error) { console.error('Key not found in localstorage or maybe wrong key.'); } } /** * It behave like native localstorage clear() function. It will clear all cache including pre-existing localstorage data. * @returns {void} void */ clearAllCache() { localStorage.clear(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.3", ngImport: i0, type: CryptoBrowserStorageService, deps: [{ token: CRYPTO_HASH_KEY }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.3", ngImport: i0, type: CryptoBrowserStorageService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.3", ngImport: i0, type: CryptoBrowserStorageService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Inject, args: [CRYPTO_HASH_KEY] }] }] }); /* * Public API Surface of crypto-browser-storage */ /** * Generated bundle index. Do not edit. */ export { CRYPTO_HASH_KEY, CryptoBrowserStorageService, SecureStorage }; //# sourceMappingURL=crypto-browser-storage.mjs.map