Rohan Sircar
4 years ago
5 changed files with 56 additions and 42 deletions
-
59src/main/frontend/chat/dto/MessageCipherDTO.ts
-
5src/main/frontend/chat/service/EncryptionServiceFactory.ts
-
26src/main/frontend/chat/service/SJCLEncryptionService.ts
-
5src/main/frontend/common/model/ChatModelHelper.ts
-
3src/main/frontend/workers/encryption-worker/main.ts
@ -1,27 +1,40 @@ |
|||
export class MessageCipherDTO { |
|||
// iv!: string;
|
|||
// v!: number;
|
|||
// iterations!: number;
|
|||
// keySize!: number;
|
|||
// tagSize!: number;
|
|||
// mode!: string;
|
|||
// adata!: string;
|
|||
// cipher!: string;
|
|||
// salt!: string;
|
|||
// cipherText!: string;
|
|||
// export class MessageCipherDTO {
|
|||
// // iv!: string;
|
|||
// // v!: number;
|
|||
// // iterations!: number;
|
|||
// // keySize!: number;
|
|||
// // tagSize!: number;
|
|||
// // mode!: string;
|
|||
// // adata!: string;
|
|||
// // cipher!: string;
|
|||
// // salt!: string;
|
|||
// // cipherText!: string;
|
|||
|
|||
iv!: string; |
|||
v!: number; |
|||
iter!: number; |
|||
ks!: number; |
|||
ts!: number; |
|||
mode!: string; |
|||
adata!: string; |
|||
cipher!: string; |
|||
salt!: string; |
|||
ct!: string; |
|||
// iv!: string;
|
|||
// v!: number;
|
|||
// iter!: number;
|
|||
// ks!: number;
|
|||
// ts!: number;
|
|||
// mode!: string;
|
|||
// adata!: string;
|
|||
// cipher!: string;
|
|||
// salt!: string;
|
|||
// ct!: string;
|
|||
|
|||
// public toMessageCipherDTO {
|
|||
// // public toMessageCipherDTO {
|
|||
|
|||
// }
|
|||
// // }
|
|||
// }
|
|||
|
|||
export interface MessageCipherDTO { |
|||
iv: string, |
|||
v: number, |
|||
iter: number, |
|||
ks: number, |
|||
ts: number, |
|||
mode: string, |
|||
adata: string, |
|||
cipher: string, |
|||
salt: string, |
|||
ct: string, |
|||
} |
@ -1,11 +1,10 @@ |
|||
import { EncryptionService } from "./EncryptionService"; |
|||
import { SJCLEncryptionService } from "./SJCLEncryptionService"; |
|||
import PromiseWorker = require('promise-worker'); |
|||
// import PromiseWorker from "promise-worker";
|
|||
// import PromiseWorker = require('promise-worker');
|
|||
import PromiseWorker from "promise-worker"; |
|||
|
|||
export class EncryptionServiceFactory { |
|||
private static readonly _worker = new Worker('/js/worker.js'); |
|||
// @ts-ignore
|
|||
private static readonly _promiseWorker = new PromiseWorker(EncryptionServiceFactory._worker); |
|||
public static getEncryptionService(): EncryptionService { |
|||
return new SJCLEncryptionService(this._promiseWorker) |
|||
|
@ -1,28 +1,32 @@ |
|||
import { EncryptionService } from "./EncryptionService"; |
|||
import * as sjcl from "sjcl"; |
|||
import { MessageCipherDTO } from "../dto/MessageCipherDTO"; |
|||
import PromiseWorker from "promise-worker"; |
|||
export class SJCLEncryptionService implements EncryptionService { |
|||
private _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 }; |
|||
// @ts-ignore
|
|||
private readonly _promiseWorker: any; |
|||
private _params: sjcl.SjclCipherParams = { mode: "gcm", ts: 128, adata: "", iter: 10000 }; |
|||
private readonly _promiseWorker: PromiseWorker; |
|||
|
|||
constructor(promiseWorker: PromiseWorker) { |
|||
this._promiseWorker = promiseWorker; |
|||
} |
|||
|
|||
constructor(promiseWorker: any ) { |
|||
this._promiseWorker = promiseWorker; |
|||
} |
|||
|
|||
|
|||
|
|||
public encrypt(passphrase: string, plainText: string): MessageCipherDTO { |
|||
// @ts-ignore
|
|||
return JSON.parse(sjcl.encrypt(passphrase, plainText, this._params) as string) as MessageCipherDTO; |
|||
const fn = () => { |
|||
// @ts-ignore
|
|||
const cipher = sjcl.encrypt(passphrase, plainText, this._params) |
|||
return cipher as unknown as string |
|||
} |
|||
return JSON.parse(fn()) |
|||
} |
|||
|
|||
public decrypt(passphrase: string, cipher: MessageCipherDTO): string { |
|||
return sjcl.decrypt(passphrase, JSON.stringify(cipher), undefined, undefined); |
|||
return sjcl.decrypt(passphrase, JSON.stringify(cipher)); |
|||
} |
|||
|
|||
public async decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string> { |
|||
const decrypted = await this._promiseWorker.postMessage({"passphrase": passphrase, "cipher": cipher}) |
|||
const decrypted = await this._promiseWorker.postMessage({ "passphrase": passphrase, "cipher": cipher }) |
|||
return decrypted; |
|||
} |
|||
} |
@ -1,8 +1,9 @@ |
|||
import registerPromiseWorker from 'promise-worker/register'; |
|||
import * as sjcl from 'sjcl' |
|||
import { MessageCipherDTO } from '../../chat/dto/MessageCipherDTO'; |
|||
|
|||
|
|||
registerPromiseWorker((payload: { passphrase: string, cipher: string }) => { |
|||
registerPromiseWorker((payload: { passphrase: string, cipher: MessageCipherDTO }) => { |
|||
// console.log(payload)
|
|||
// console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)));
|
|||
return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue