34 lines
856 B
TypeScript
34 lines
856 B
TypeScript
import CryptoJS from 'crypto-js';
|
|
import { config } from '../config';
|
|
|
|
export class EncryptionService {
|
|
private static instance: EncryptionService;
|
|
private readonly key: string;
|
|
|
|
private constructor() {
|
|
this.key = config.encryption.key;
|
|
}
|
|
|
|
static getInstance(): EncryptionService {
|
|
if (!EncryptionService.instance) {
|
|
EncryptionService.instance = new EncryptionService();
|
|
}
|
|
return EncryptionService.instance;
|
|
}
|
|
|
|
encrypt(plainText: string): string {
|
|
return CryptoJS.AES.encrypt(plainText, this.key).toString();
|
|
}
|
|
|
|
decrypt(cipherText: string): string {
|
|
const bytes = CryptoJS.AES.decrypt(cipherText, this.key);
|
|
return bytes.toString(CryptoJS.enc.Utf8);
|
|
}
|
|
|
|
hash(data: string): string {
|
|
return CryptoJS.SHA256(data).toString();
|
|
}
|
|
}
|
|
|
|
export const encryptionService = EncryptionService.getInstance();
|