Files
AngularFirebaseVotingApp/src/app/services/local-storage.service.ts
2022-11-03 13:26:26 +01:00

35 lines
813 B
TypeScript

import { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
@Injectable({
providedIn: 'root'
})
export class LocalStorageService {
private key = "AngularFirebaseVotingAPP_Key@0";
constructor() { }
public saveData(key: string, value: string) {
localStorage.setItem(key, this.encrypt(value));
}
public getData(key: string) {
let data = localStorage.getItem(key)|| "";
return this.decrypt(data);
}
public removeData(key: string) {
localStorage.removeItem(key);
}
public clearData() {
localStorage.clear();
}
private encrypt(txt: string): string {
return CryptoJS.AES.encrypt(txt, this.key).toString();
}
private decrypt(txtToDecrypt: string) {
return CryptoJS.AES.decrypt(txtToDecrypt, this.key).toString(CryptoJS.enc.Utf8);
}
}