|
|
@ -1,15 +1,24 @@ |
|
|
|
import { EncryptionService } from "./EncryptionService"; |
|
|
|
import * as sjcl from "sjcl"; |
|
|
|
import { MessageCipherDTO } from "../dto/MessageCipherDTO"; |
|
|
|
|
|
|
|
import PromiseWorker = require('promise-worker'); |
|
|
|
export class SJCLEncryptionService implements EncryptionService { |
|
|
|
private readonly params = { mode: "gcm", ts: 128, adata: "", iter: 10000 } |
|
|
|
private readonly _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 }; |
|
|
|
private readonly _worker = new Worker('/js/worker.js'); |
|
|
|
// @ts-ignore
|
|
|
|
private readonly _promiseWorker = new PromiseWorker(this._worker); |
|
|
|
|
|
|
|
public encrypt(passphrase: string, plainText: string): MessageCipherDTO { |
|
|
|
// @ts-ignore
|
|
|
|
return JSON.parse(sjcl.encrypt(passphrase, plainText, this.params) as string) as MessageCipherDTO; |
|
|
|
return JSON.parse(sjcl.encrypt(passphrase, plainText, this._params) as string) as MessageCipherDTO; |
|
|
|
} |
|
|
|
|
|
|
|
public decrypt(passphrase: string, cipher: MessageCipherDTO): string { |
|
|
|
return sjcl.decrypt(passphrase, JSON.stringify(cipher), undefined, undefined); |
|
|
|
} |
|
|
|
|
|
|
|
public async decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string> { |
|
|
|
const decrypted = await this._promiseWorker.postMessage({"passphrase": passphrase, "cipher": cipher}) |
|
|
|
return decrypted; |
|
|
|
} |
|
|
|
} |