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.

23 lines
1.1 KiB

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