Compare commits
2 Commits
53535d68e0
...
5741097334
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5741097334 | ||
|
|
95a1f57381 |
@@ -1,115 +0,0 @@
|
||||
import { sha256, md5 } from "node-forge";
|
||||
import { generateRandomKey } from "../utils/utils";
|
||||
import * as secp256k1 from '@noble/secp256k1';
|
||||
|
||||
const worker = new Worker(new URL('./crypto.worker.ts', import.meta.url), { type: 'module' });
|
||||
|
||||
export const encodeWithPassword = async (password : string, data : any) : Promise<any> => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, _) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'encodeWithPasswordResult' && event.data.task === task) {
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'encodeWithPassword', data: { password, payload: data, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const decodeWithPassword = (password : string, data : any) : Promise<any> => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, reject) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'decodeWithPasswordResult' && event.data.task === task) {
|
||||
if(event.data.result === null){
|
||||
reject("Decryption failed");
|
||||
return;
|
||||
}
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'decodeWithPassword', data: { password, payload: data, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const generateKeyPairFromSeed = async (seed : string) => {
|
||||
//generate key pair using secp256k1 includes privatekey from seed
|
||||
const privateKey = sha256.create().update(seed).digest().toHex().toString();
|
||||
const publicKey = secp256k1.getPublicKey(Buffer.from(privateKey, "hex"), true);
|
||||
return {
|
||||
privateKey: privateKey,
|
||||
publicKey: Buffer.from(publicKey).toString('hex'),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export const encrypt = async (data : string, publicKey : string) : Promise<any> => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, _) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'encryptResult' && event.data.task === task) {
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'encrypt', data: { publicKey, payload: data, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const decrypt = async (data : string, privateKey : string) : Promise<any> => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, reject) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'decryptResult' && event.data.task === task) {
|
||||
if(event.data.result === null){
|
||||
reject("Decryption failed");
|
||||
return;
|
||||
}
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'decrypt', data: { privateKey, payload: data, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const chacha20Encrypt = async (data : string) => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, _) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'chacha20EncryptResult' && event.data.task === task) {
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'chacha20Encrypt', data: { payload: data, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const chacha20Decrypt = async (ciphertext : string, nonce : string, key : string) => {
|
||||
let task = generateRandomKey(16);
|
||||
return new Promise((resolve, _) => {
|
||||
worker.addEventListener('message', (event: MessageEvent) => {
|
||||
if (event.data.action === 'chacha20DecryptResult' && event.data.task === task) {
|
||||
resolve(event.data.result);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ action: 'chacha20Decrypt', data: { ciphertext, nonce, key, task } });
|
||||
});
|
||||
}
|
||||
|
||||
export const generateMd5 = async (data : string) => {
|
||||
const hash = md5.create();
|
||||
hash.update(data);
|
||||
return hash.digest().toHex();
|
||||
}
|
||||
|
||||
export const generateHashFromPrivateKey = async (privateKey : string) => {
|
||||
return sha256.create().update(privateKey + "rosetta").digest().toHex().toString();
|
||||
}
|
||||
|
||||
export const isEncodedWithPassword = (data : string) => {
|
||||
try{
|
||||
atob(data).split(":");
|
||||
return true;
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { useUploadStatus } from "../TransportProvider/useUploadStatus";
|
||||
import { useFileStorage } from "../../hooks/useFileStorage";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
import { usePrivatePlain } from "../AccountProvider/usePrivatePlain";
|
||||
import { decodeWithPassword, encodeWithPassword, generateMd5 } from "../../crypto/crypto";
|
||||
import { decodeWithPassword, encodeWithPassword, generateMd5 } from "../../workers/crypto/crypto";
|
||||
import { useTransport } from "../TransportProvider/useTransport";
|
||||
import { useDialogsCache } from "../DialogProvider/useDialogsCache";
|
||||
import { useConsoleLogger } from "../../hooks/useConsoleLogger";
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import { createContext, useEffect, useRef, useState } from "react";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
import { useFileStorage } from "@/app/hooks/useFileStorage";
|
||||
import { decodeWithPassword, encodeWithPassword, generateMd5 } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword, encodeWithPassword, generateMd5 } from "@/app/workers/crypto/crypto";
|
||||
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
||||
import { useSystemAccounts } from "../SystemAccountsProvider/useSystemAccounts";
|
||||
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
|
||||
import { DialogRow } from "./DialogListProvider";
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
import { decodeWithPassword } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword } from "@/app/workers/crypto/crypto";
|
||||
import { constructLastMessageTextByAttachments } from "@/app/utils/constructLastMessageTextByAttachments";
|
||||
import { usePrivatePlain } from "../AccountProvider/usePrivatePlain";
|
||||
import { DeliveredMessageState, Message } from "../DialogProvider/DialogProvider";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { chacha20Decrypt, decodeWithPassword, decrypt, encodeWithPassword, generateMd5 } from '@/app/crypto/crypto';
|
||||
import { chacha20Decrypt, decodeWithPassword, decrypt, encodeWithPassword, generateMd5 } from '@/app/workers/crypto/crypto';
|
||||
import { useDatabase } from '@/app/providers/DatabaseProvider/useDatabase';
|
||||
import { createContext, useEffect, useRef, useState } from 'react';
|
||||
import { Attachment, AttachmentType, PacketMessage } from '@/app/providers/ProtocolProvider/protocol/packets/packet.message';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useContext } from "react";
|
||||
import { useDatabase } from "../DatabaseProvider/useDatabase";
|
||||
import { chacha20Encrypt, encodeWithPassword, encrypt, generateMd5} from "../../crypto/crypto";
|
||||
import { chacha20Encrypt, encodeWithPassword, encrypt, generateMd5} from "../../workers/crypto/crypto";
|
||||
import { AttachmentMeta, DeliveredMessageState, DialogContext, Message } from "./DialogProvider";
|
||||
import { Attachment, AttachmentType, PacketMessage } from "@/app/providers/ProtocolProvider/protocol/packets/packet.message";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
|
||||
@@ -12,7 +12,7 @@ import { useDialogsCache } from "./useDialogsCache";
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import { usePrivatePlain } from "../AccountProvider/usePrivatePlain";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
import { chacha20Decrypt, decodeWithPassword, decrypt, encodeWithPassword, generateMd5 } from "@/app/crypto/crypto";
|
||||
import { chacha20Decrypt, decodeWithPassword, decrypt, encodeWithPassword, generateMd5 } from "@/app/workers/crypto/crypto";
|
||||
import { DeliveredMessageState, Message } from "./DialogProvider";
|
||||
import { PacketRead } from "../ProtocolProvider/protocol/packets/packet.read";
|
||||
import { PacketDelivery } from "../ProtocolProvider/protocol/packets/packet.delivery";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import { usePrivatePlain } from "../AccountProvider/usePrivatePlain";
|
||||
import { decodeWithPassword, encodeWithPassword } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword, encodeWithPassword } from "@/app/workers/crypto/crypto";
|
||||
import { generateRandomKey } from "@/app/utils/utils";
|
||||
import { useDialogsList } from "../DialogListProvider/useDialogsList";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createContext } from "react";
|
||||
import { useSystemAccount } from "./useSystemAccount";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
import { usePrivatePlain } from "../AccountProvider/usePrivatePlain";
|
||||
import { chacha20Encrypt, encodeWithPassword, encrypt } from "@/app/crypto/crypto";
|
||||
import { chacha20Encrypt, encodeWithPassword, encrypt } from "@/app/workers/crypto/crypto";
|
||||
import { generateRandomKey } from "@/app/utils/utils";
|
||||
import { DeliveredMessageState } from "../DialogProvider/DialogProvider";
|
||||
import { UserInformation } from "../InformationProvider/InformationProvider";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { decodeWithPassword, encodeWithPassword } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword, encodeWithPassword } from "@/app/workers/crypto/crypto";
|
||||
import { useFileStorage } from "@/app/hooks/useFileStorage";
|
||||
import { generateRandomKey } from "@/app/utils/utils";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
|
||||
@@ -3,7 +3,7 @@ import { InternalScreen } from "@/app/components/InternalScreen/InternalScreen";
|
||||
import { SettingsAlert } from "@/app/components/SettingsAlert/SettingsAlert";
|
||||
import { SettingsInput } from "@/app/components/SettingsInput/SettingsInput";
|
||||
import { TextChain } from "@/app/components/TextChain/TextChain";
|
||||
import { decodeWithPassword } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword } from "@/app/workers/crypto/crypto";
|
||||
import { useAccount } from "@/app/providers/AccountProvider/useAccount";
|
||||
import { Text } from "@mantine/core";
|
||||
import { useState } from "react";
|
||||
|
||||
@@ -3,7 +3,7 @@ import classes from './Lockscreen.module.css'
|
||||
import { useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import useWindow from "@/app/hooks/useWindow";
|
||||
import { decodeWithPassword, generateHashFromPrivateKey } from "@/app/crypto/crypto";
|
||||
import { decodeWithPassword, generateHashFromPrivateKey } from "@/app/workers/crypto/crypto";
|
||||
import { useAccountProvider } from "@/app/providers/AccountProvider/useAccountProvider";
|
||||
import { Account, AccountBase } from "@/app/providers/AccountProvider/AccountProvider";
|
||||
import { useUserCache } from "@/app/providers/InformationProvider/useUserCache";
|
||||
|
||||
@@ -7,7 +7,7 @@ import { mnemonicToSeed } from "web-bip39";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { modals } from "@mantine/modals";
|
||||
import { Buffer } from 'buffer'
|
||||
import { encodeWithPassword, generateHashFromPrivateKey, generateKeyPairFromSeed } from "@/app/crypto/crypto";
|
||||
import { encodeWithPassword, generateHashFromPrivateKey, generateKeyPairFromSeed } from "@/app/workers/crypto/crypto";
|
||||
import { useAccountProvider } from "@/app/providers/AccountProvider/useAccountProvider";
|
||||
import { Account } from "@/app/providers/AccountProvider/AccountProvider";
|
||||
import { useRosettaColors } from "@/app/hooks/useRosettaColors";
|
||||
|
||||
88
app/workers/crypto/crypto.ts
Normal file
88
app/workers/crypto/crypto.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { sha256, md5 } from "node-forge";
|
||||
import * as secp256k1 from '@noble/secp256k1';
|
||||
|
||||
const worker = new Worker(new URL('./crypto.worker.ts', import.meta.url), { type: 'module' });
|
||||
|
||||
type WorkerReq =
|
||||
| { id: number; type: 'encodeWithPassword'; payload: { password: string; data: any } }
|
||||
| { id: number; type: 'decodeWithPassword'; payload: { password: string; data: any } }
|
||||
| { id: number; type: 'encrypt'; payload: { publicKey: string; data: string } }
|
||||
| { id: number; type: 'decrypt'; payload: { privateKey: string; data: string } }
|
||||
| { id: number; type: 'chacha20Encrypt'; payload: { data: string } }
|
||||
| { id: number; type: 'chacha20Decrypt'; payload: { ciphertext: string; nonce: string; key: string } };
|
||||
|
||||
type WorkerRes =
|
||||
| { id: number; ok: true; data: any }
|
||||
| { id: number; ok: false; error: string };
|
||||
|
||||
let seq = 0;
|
||||
const pending = new Map<number, (res: WorkerRes) => void>();
|
||||
|
||||
worker.onmessage = (e: MessageEvent<WorkerRes>) => {
|
||||
const res = e.data;
|
||||
const cb = pending.get(res.id);
|
||||
if (cb) {
|
||||
pending.delete(res.id);
|
||||
cb(res);
|
||||
}
|
||||
};
|
||||
|
||||
function callWorker(req: Omit<WorkerReq, 'id'>): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = ++seq;
|
||||
pending.set(id, (res) => (res.ok ? resolve(res.data) : reject(res.error)));
|
||||
worker.postMessage({ ...req, id });
|
||||
});
|
||||
}
|
||||
|
||||
export const encodeWithPassword = (password: string, data: any): Promise<any> => {
|
||||
return callWorker({ type: 'encodeWithPassword', payload: { password, data } });
|
||||
};
|
||||
|
||||
export const decodeWithPassword = (password: string, data: any): Promise<any> => {
|
||||
return callWorker({ type: 'decodeWithPassword', payload: { password, data } });
|
||||
};
|
||||
|
||||
export const encrypt = (data: string, publicKey: string): Promise<any> => {
|
||||
return callWorker({ type: 'encrypt', payload: { publicKey, data } });
|
||||
};
|
||||
|
||||
export const decrypt = (data: string, privateKey: string): Promise<any> => {
|
||||
return callWorker({ type: 'decrypt', payload: { privateKey, data } });
|
||||
};
|
||||
|
||||
export const chacha20Encrypt = (data: string): Promise<any> => {
|
||||
return callWorker({ type: 'chacha20Encrypt', payload: { data } });
|
||||
};
|
||||
|
||||
export const chacha20Decrypt = (ciphertext: string, nonce: string, key: string): Promise<any> => {
|
||||
return callWorker({ type: 'chacha20Decrypt', payload: { ciphertext, nonce, key } });
|
||||
};
|
||||
|
||||
export const generateKeyPairFromSeed = async (seed: string) => {
|
||||
const privateKey = sha256.create().update(seed).digest().toHex().toString();
|
||||
const publicKey = secp256k1.getPublicKey(Buffer.from(privateKey, "hex"), true);
|
||||
return {
|
||||
privateKey: privateKey,
|
||||
publicKey: Buffer.from(publicKey).toString('hex'),
|
||||
};
|
||||
};
|
||||
|
||||
export const generateMd5 = async (data: string) => {
|
||||
const hash = md5.create();
|
||||
hash.update(data);
|
||||
return hash.digest().toHex();
|
||||
};
|
||||
|
||||
export const generateHashFromPrivateKey = async (privateKey: string) => {
|
||||
return sha256.create().update(privateKey + "rosetta").digest().toHex().toString();
|
||||
};
|
||||
|
||||
export const isEncodedWithPassword = (data: string) => {
|
||||
try {
|
||||
atob(data).split(":");
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -6,53 +6,35 @@ import * as secp256k1 from '@noble/secp256k1';
|
||||
|
||||
|
||||
self.onmessage = async (event: MessageEvent) => {
|
||||
const { action, data } = event.data;
|
||||
const { id, type, payload } = event.data;
|
||||
|
||||
switch (action) {
|
||||
case 'encodeWithPassword': {
|
||||
const { password, payload, task } = data;
|
||||
const result = await encodeWithPassword(password, payload);
|
||||
self.postMessage({ action: 'encodeWithPasswordResult', result, task });
|
||||
try {
|
||||
let result;
|
||||
switch (type) {
|
||||
case 'encodeWithPassword':
|
||||
result = await encodeWithPassword(payload.password, payload.data);
|
||||
break;
|
||||
}
|
||||
case 'chacha20Encrypt': {
|
||||
const { payload, task } = data;
|
||||
const result = await chacha20Encrypt(payload);
|
||||
self.postMessage({ action: 'chacha20EncryptResult', result, task });
|
||||
case 'decodeWithPassword':
|
||||
result = await decodeWithPassword(payload.password, payload.data);
|
||||
break;
|
||||
}
|
||||
case 'chacha20Decrypt': {
|
||||
const { ciphertext, nonce, key, task } = data;
|
||||
const result = await chacha20Decrypt(ciphertext, nonce, key);
|
||||
self.postMessage({ action: 'chacha20DecryptResult', result, task });
|
||||
case 'encrypt':
|
||||
result = await encrypt(payload.publicKey, payload.data);
|
||||
break;
|
||||
}
|
||||
case 'decodeWithPassword': {
|
||||
const { password, payload, task } = data;
|
||||
try{
|
||||
const result = await decodeWithPassword(password, payload);
|
||||
self.postMessage({ action: 'decodeWithPasswordResult', result, task });
|
||||
return;
|
||||
}catch(e){
|
||||
const result = null;
|
||||
self.postMessage({ action: 'decodeWithPasswordResult', result, task });
|
||||
}
|
||||
case 'decrypt':
|
||||
result = await decrypt(payload.privateKey, payload.data);
|
||||
break;
|
||||
}
|
||||
case 'decrypt': {
|
||||
const { payload: encryptedData, privateKey, task } = data;
|
||||
const result = await decrypt(encryptedData, privateKey);
|
||||
self.postMessage({ action: 'decryptResult', result, task });
|
||||
case 'chacha20Encrypt':
|
||||
result = await chacha20Encrypt(payload.data);
|
||||
break;
|
||||
}
|
||||
case 'encrypt': {
|
||||
const { payload: plainData, publicKey, task } = data;
|
||||
const result = await encrypt(plainData, publicKey);
|
||||
self.postMessage({ action: 'encryptResult', result, task });
|
||||
case 'chacha20Decrypt':
|
||||
result = await chacha20Decrypt(payload.ciphertext, payload.nonce, payload.key);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.error(`Unknown action: ${action}`);
|
||||
throw new Error(`Unknown action: ${type}`);
|
||||
}
|
||||
self.postMessage({ id, ok: true, data: result });
|
||||
} catch (error) {
|
||||
self.postMessage({ id, ok: false, error: String(error) });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user