Made cipher dto an interface

This commit is contained in:
Rohan Sircar 2020-06-20 19:38:05 +05:30
parent 0d78d057bc
commit 8450d8b864
5 changed files with 56 additions and 42 deletions

View File

@ -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,
}

View File

@ -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)

View File

@ -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;
}
}

View File

@ -88,10 +88,7 @@ export class ChatModelHelper {
const vm = new ChatMessageViewModel();
vm.fromUser = chatMessageDTO.fromUser;
vm.toUser = chatMessageDTO.toUser;
// vm.messageTime = chatMessageDTO.messageTime;
chatMessageDTO.messageTime == null
? log.error("Message time somehow null")
: (vm.messageTime = chatMessageDTO.messageTime);
vm.messageTime = chatMessageDTO.messageTime;
vm.message = await this._encryptionService.decryptAsPromise(
passphrase,
chatMessageDTO.messageCipher

View File

@ -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))