A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1.0 KiB

  1. import { EncryptionService } from "./EncryptionService";
  2. import * as sjcl from "sjcl";
  3. import { MessageCipherDTO } from "../dto/MessageCipherDTO";
  4. export class SJCLEncryptionService implements EncryptionService {
  5. private _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 };
  6. // @ts-ignore
  7. private readonly _promiseWorker: any;
  8. constructor(promiseWorker: any ) {
  9. this._promiseWorker = promiseWorker;
  10. }
  11. public encrypt(passphrase: string, plainText: string): MessageCipherDTO {
  12. // @ts-ignore
  13. return JSON.parse(sjcl.encrypt(passphrase, plainText, this._params) as string) as MessageCipherDTO;
  14. }
  15. public decrypt(passphrase: string, cipher: MessageCipherDTO): string {
  16. return sjcl.decrypt(passphrase, JSON.stringify(cipher), undefined, undefined);
  17. }
  18. public async decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string> {
  19. const decrypted = await this._promiseWorker.postMessage({"passphrase": passphrase, "cipher": cipher})
  20. return decrypted;
  21. }
  22. }