Made cipher dto an interface
This commit is contained in:
parent
0d78d057bc
commit
8450d8b864
@ -1,27 +1,40 @@
|
|||||||
export class MessageCipherDTO {
|
// 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;
|
// iv!: string;
|
||||||
// v!: number;
|
// v!: number;
|
||||||
// iterations!: number;
|
// iter!: number;
|
||||||
// keySize!: number;
|
// ks!: number;
|
||||||
// tagSize!: number;
|
// ts!: number;
|
||||||
// mode!: string;
|
// mode!: string;
|
||||||
// adata!: string;
|
// adata!: string;
|
||||||
// cipher!: string;
|
// cipher!: string;
|
||||||
// salt!: string;
|
// salt!: string;
|
||||||
// cipherText!: string;
|
// ct!: string;
|
||||||
|
|
||||||
iv!: string;
|
// // public toMessageCipherDTO {
|
||||||
v!: number;
|
|
||||||
iter!: number;
|
|
||||||
ks!: number;
|
|
||||||
ts!: number;
|
|
||||||
mode!: string;
|
|
||||||
adata!: string;
|
|
||||||
cipher!: string;
|
|
||||||
salt!: string;
|
|
||||||
ct!: string;
|
|
||||||
|
|
||||||
// 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 { EncryptionService } from "./EncryptionService";
|
||||||
import { SJCLEncryptionService } from "./SJCLEncryptionService";
|
import { SJCLEncryptionService } from "./SJCLEncryptionService";
|
||||||
import PromiseWorker = require('promise-worker');
|
// import PromiseWorker = require('promise-worker');
|
||||||
// import PromiseWorker from "promise-worker";
|
import PromiseWorker from "promise-worker";
|
||||||
|
|
||||||
export class EncryptionServiceFactory {
|
export class EncryptionServiceFactory {
|
||||||
private static readonly _worker = new Worker('/js/worker.js');
|
private static readonly _worker = new Worker('/js/worker.js');
|
||||||
// @ts-ignore
|
|
||||||
private static readonly _promiseWorker = new PromiseWorker(EncryptionServiceFactory._worker);
|
private static readonly _promiseWorker = new PromiseWorker(EncryptionServiceFactory._worker);
|
||||||
public static getEncryptionService(): EncryptionService {
|
public static getEncryptionService(): EncryptionService {
|
||||||
return new SJCLEncryptionService(this._promiseWorker)
|
return new SJCLEncryptionService(this._promiseWorker)
|
||||||
|
@ -1,24 +1,28 @@
|
|||||||
import { EncryptionService } from "./EncryptionService";
|
import { EncryptionService } from "./EncryptionService";
|
||||||
import * as sjcl from "sjcl";
|
import * as sjcl from "sjcl";
|
||||||
import { MessageCipherDTO } from "../dto/MessageCipherDTO";
|
import { MessageCipherDTO } from "../dto/MessageCipherDTO";
|
||||||
|
import PromiseWorker from "promise-worker";
|
||||||
export class SJCLEncryptionService implements EncryptionService {
|
export class SJCLEncryptionService implements EncryptionService {
|
||||||
private _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 };
|
private _params: sjcl.SjclCipherParams = { mode: "gcm", ts: 128, adata: "", iter: 10000 };
|
||||||
// @ts-ignore
|
private readonly _promiseWorker: PromiseWorker;
|
||||||
private readonly _promiseWorker: any;
|
|
||||||
|
|
||||||
constructor(promiseWorker: any ) {
|
constructor(promiseWorker: PromiseWorker) {
|
||||||
this._promiseWorker = promiseWorker;
|
this._promiseWorker = promiseWorker;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public encrypt(passphrase: string, plainText: string): MessageCipherDTO {
|
public encrypt(passphrase: string, plainText: string): MessageCipherDTO {
|
||||||
|
const fn = () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return JSON.parse(sjcl.encrypt(passphrase, plainText, this._params) as string) as MessageCipherDTO;
|
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 {
|
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> {
|
public async decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string> {
|
||||||
|
@ -88,10 +88,7 @@ export class ChatModelHelper {
|
|||||||
const vm = new ChatMessageViewModel();
|
const vm = new ChatMessageViewModel();
|
||||||
vm.fromUser = chatMessageDTO.fromUser;
|
vm.fromUser = chatMessageDTO.fromUser;
|
||||||
vm.toUser = chatMessageDTO.toUser;
|
vm.toUser = chatMessageDTO.toUser;
|
||||||
// vm.messageTime = chatMessageDTO.messageTime;
|
vm.messageTime = chatMessageDTO.messageTime;
|
||||||
chatMessageDTO.messageTime == null
|
|
||||||
? log.error("Message time somehow null")
|
|
||||||
: (vm.messageTime = chatMessageDTO.messageTime);
|
|
||||||
vm.message = await this._encryptionService.decryptAsPromise(
|
vm.message = await this._encryptionService.decryptAsPromise(
|
||||||
passphrase,
|
passphrase,
|
||||||
chatMessageDTO.messageCipher
|
chatMessageDTO.messageCipher
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import registerPromiseWorker from 'promise-worker/register';
|
import registerPromiseWorker from 'promise-worker/register';
|
||||||
import * as sjcl from 'sjcl'
|
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(payload)
|
||||||
// console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)));
|
// console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)));
|
||||||
return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher))
|
return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher))
|
||||||
|
Loading…
Reference in New Issue
Block a user