2023-10-23 04:39:35 -07:00
|
|
|
import { Service } from 'typedi';
|
|
|
|
import { AES, enc } from 'crypto-js';
|
|
|
|
import { InstanceSettings } from './InstanceSettings';
|
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class Cipher {
|
|
|
|
constructor(private readonly instanceSettings: InstanceSettings) {}
|
|
|
|
|
|
|
|
encrypt(data: string | object) {
|
2023-10-26 02:21:53 -07:00
|
|
|
const { encryptionKey } = this.instanceSettings;
|
2023-10-23 04:39:35 -07:00
|
|
|
return AES.encrypt(
|
|
|
|
typeof data === 'string' ? data : JSON.stringify(data),
|
2023-10-26 02:21:53 -07:00
|
|
|
encryptionKey,
|
2023-10-23 04:39:35 -07:00
|
|
|
).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypt(data: string) {
|
2023-10-26 02:21:53 -07:00
|
|
|
const { encryptionKey } = this.instanceSettings;
|
|
|
|
return AES.decrypt(data, encryptionKey).toString(enc.Utf8);
|
2023-10-23 04:39:35 -07:00
|
|
|
}
|
|
|
|
}
|