made a factory for encryption service
This commit is contained in:
parent
97a89e5624
commit
ff33817930
@ -31,6 +31,7 @@ import { UserViewDeps } from "./view/UserViewDeps";
|
||||
import { ChatViewDeps } from "./view/ChatViewDeps";
|
||||
import { MarkDownItMarkDownService } from "./service/MarkDownItMarkDownService";
|
||||
import { Sprintf } from "./singleton/Sprintf";
|
||||
import { EncryptionServiceFactory } from "./service/EncryptionServiceFactory";
|
||||
|
||||
const usersListElement = document.getElementById('contacts-box');
|
||||
const userSearchButton = document.getElementById('user-search');
|
||||
@ -84,7 +85,7 @@ const cvDeps: ChatViewDeps = {
|
||||
messageSendTemplate: TemplateFactory.getTemplate('msg_container_send_template'),
|
||||
messageReceiveTemplate: TemplateFactory.getTemplate('msg_container_template'),
|
||||
markdownService: new MarkDownItMarkDownService,
|
||||
encryptionService: new SJCLEncryptionService
|
||||
encryptionService: EncryptionServiceFactory.getEncryptionService()
|
||||
|
||||
}
|
||||
const chatView = new ChatView(cvDeps);
|
||||
@ -109,7 +110,7 @@ var msgContainerTemplate = Handlebars.compile(source);
|
||||
|
||||
JsonAPI.ACTIVE_USERS_GET + 'aef';
|
||||
|
||||
const encryptionService: EncryptionService = new SJCLEncryptionService();
|
||||
const encryptionService: EncryptionService = EncryptionServiceFactory.getEncryptionService();
|
||||
let messageCipherDTO: MessageCipherDTO = encryptionService.encrypt("password", "data");
|
||||
console.log(encryptionService.decrypt("password", messageCipherDTO));
|
||||
|
||||
|
@ -75,7 +75,7 @@ export class ChatModel implements Subject {
|
||||
// log.debug('page number after = ' + this._messagePageMap.get(contactName)!)
|
||||
// }
|
||||
const pageNumber = this._messagePageMap.get(contactName)
|
||||
const cVMs = await ChatModelHelper.getMessages(contactName, passphrase, pageNumber!, lastMessageTime, this);
|
||||
const cVMs = await ChatModelHelper.getMessages(contactName, passphrase, pageNumber!, lastMessageTime);
|
||||
if (cVMs != null) {
|
||||
log.info('Subject: My state has just changed')
|
||||
|
||||
|
@ -1,23 +1,21 @@
|
||||
import * as log from "loglevel";
|
||||
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
|
||||
import { EncryptionService } from "../service/EncryptionService";
|
||||
import { SJCLEncryptionService } from "../service/SJCLEncryptionService";
|
||||
import { EncryptionServiceFactory } from "../service/EncryptionServiceFactory";
|
||||
import { JsonAPI } from "../singleton/JsonAPI";
|
||||
import { Sprintf } from "../singleton/Sprintf";
|
||||
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
|
||||
import { ChatModel } from "./ChatModel";
|
||||
import { fetchErrorHandler } from "./FetchErrorHandler";
|
||||
|
||||
export class ChatModelHelper {
|
||||
private static readonly _encryptionService: EncryptionService = new SJCLEncryptionService();
|
||||
private static readonly _encryptionService: EncryptionService = EncryptionServiceFactory.getEncryptionService();
|
||||
|
||||
public static async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null, chatModel: ChatModel): Promise<ChatMessageViewModel[]> {
|
||||
public static async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null): Promise<ChatMessageViewModel[]> {
|
||||
switch (lastMessageTime) {
|
||||
case null: {
|
||||
const data: ChatMessageDTO[] = await this.getPaginatedMessagesAjax(userName, page!);
|
||||
const data2 = Promise.all(data.map(vm => this.toChatMessageVMAsync(vm, passphrase)).reverse());
|
||||
return data2;
|
||||
// return data.map(vm => this.toChatMessageVM(vm, passphrase)).reverse();
|
||||
const cVMs = Promise.all(data.map(vm => this.toChatMessageVMAsync(vm, passphrase)).reverse());
|
||||
return cVMs;
|
||||
}
|
||||
default: {
|
||||
const data: ChatMessageDTO[] = await this.getNewMessagesAjax(userName, lastMessageTime);
|
||||
|
@ -0,0 +1,13 @@
|
||||
import { EncryptionService } from "./EncryptionService";
|
||||
import { SJCLEncryptionService } from "./SJCLEncryptionService";
|
||||
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,12 +1,16 @@
|
||||
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 _worker = new Worker('/js/worker.js');
|
||||
private _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 };
|
||||
// @ts-ignore
|
||||
private readonly _promiseWorker = new PromiseWorker(this._worker);
|
||||
private readonly _promiseWorker: any;
|
||||
|
||||
constructor(promiseWorker: any ) {
|
||||
this._promiseWorker = promiseWorker;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public encrypt(passphrase: string, plainText: string): MessageCipherDTO {
|
||||
// @ts-ignore
|
||||
|
@ -1,10 +1,10 @@
|
||||
// worker.js
|
||||
importScripts('https://unpkg.com/promise-worker/dist/promise-worker.register.js');
|
||||
// importScripts('https://unpkg.com/promise-worker@2.0.1/dist/promise-worker.register.js')
|
||||
importScripts('https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.8/sjcl.min.js');
|
||||
|
||||
registerPromiseWorker((payload) => {
|
||||
// console.log(payload)
|
||||
// console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)));
|
||||
// return 'pong';
|
||||
return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher))
|
||||
});
|
Loading…
Reference in New Issue
Block a user