Унификация кодирования chacha_key_plain теперь он в hex, а не в utf8 с потерей данных

This commit is contained in:
RoyceDa
2026-03-28 17:52:13 +02:00
parent f5bfa153b6
commit 61e83bdd43
8 changed files with 23 additions and 23 deletions

View File

@@ -42,7 +42,7 @@ export function MessageReplyMessages(props: AttachmentProps) {
<Skeleton h={50} w={'100%'}></Skeleton> <Skeleton h={50} w={'100%'}></Skeleton>
} }
{reply.map((msg : MessageReply, index) => ( {reply.map((msg : MessageReply, index) => (
<ReplyedMessage parent={props.parent} chacha_key_plain={Buffer.from(msg.chacha_key_plain, 'hex').toString('utf-8')} key={index} messageReply={msg}></ReplyedMessage> <ReplyedMessage parent={props.parent} chacha_key_plain={msg.chacha_key_plain} key={index} messageReply={msg}></ReplyedMessage>
))} ))}
{showAlertInReplyMessages && <Alert style={{ {showAlertInReplyMessages && <Alert style={{
borderTopLeftRadius: 0, borderTopLeftRadius: 0,

View File

@@ -104,6 +104,9 @@ export function Message(props: MessageProps) {
message: props.message, message: props.message,
attachments: props.attachments.filter(a => a.type != AttachmentType.MESSAGES), attachments: props.attachments.filter(a => a.type != AttachmentType.MESSAGES),
message_id: props.message_id, message_id: props.message_id,
/**
* Кодируем в hex чтобы было удобнее передавать по сети
*/
chacha_key_plain: props.chacha_key_plain chacha_key_plain: props.chacha_key_plain
}; };

View File

@@ -221,7 +221,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если это сообщение от нас, то проверяем, есть ли внутри chacha_key * Если это сообщение от нас, то проверяем, есть ли внутри chacha_key
*/ */
try{ try{
decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key), 'binary').toString('utf-8'); decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key), 'binary').toString('hex');
}catch(e) { }catch(e) {
decryptKey = ""; decryptKey = "";
} }
@@ -232,7 +232,7 @@ export function DialogProvider(props: DialogProviderProps) {
* сообщение пришло нам в результате синхронизации и его нужно расшифровать, если chacha_key нет, * сообщение пришло нам в результате синхронизации и его нужно расшифровать, если chacha_key нет,
* значит сообщение отправлено с нашего устройства, и зашифровано на стороне отправки (plain_message) * значит сообщение отправлено с нашего устройства, и зашифровано на стороне отправки (plain_message)
*/ */
decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key.replace("sync:", "")), 'binary').toString('utf-8'); decryptKey = Buffer.from(await decodeWithPassword(privatePlain, message.chacha_key.replace("sync:", "")), 'binary').toString('hex');
} }
if(hasGroup(props.dialog)){ if(hasGroup(props.dialog)){
/** /**
@@ -245,7 +245,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,
@@ -482,7 +482,7 @@ export function DialogProvider(props: DialogProviderProps) {
const attachment = packet.getAttachments()[i]; const attachment = packet.getAttachments()[i];
attachments.push({ attachments.push({
...attachment, ...attachment,
blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob) : "" blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob) : ""
}); });
} }
@@ -492,7 +492,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: 0, //сообщение прочитано readed: 0, //сообщение прочитано
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: 1, //сообщение от нас from_me: 1, //сообщение от нас
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -636,7 +636,7 @@ export function DialogProvider(props: DialogProviderProps) {
const attachment = packet.getAttachments()[i]; const attachment = packet.getAttachments()[i];
attachments.push({ attachments.push({
...attachment, ...attachment,
blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob) : "" blob: attachment.type == AttachmentType.MESSAGES ? await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob) : ""
}); });
} }
console.info(attachments); console.info(attachments);
@@ -646,7 +646,7 @@ export function DialogProvider(props: DialogProviderProps) {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,
@@ -798,7 +798,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,
@@ -883,7 +883,7 @@ export function DialogProvider(props: DialogProviderProps) {
* Если сообщение не от меня и не групповое, * Если сообщение не от меня и не групповое,
* расшифровываем ключ чачи своим приватным ключом * расшифровываем ключ чачи своим приватным ключом
*/ */
decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8'); decryptKey = Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('hex');
} }
finalMessages.push({ finalMessages.push({
from_public_key: message.from_public_key, from_public_key: message.from_public_key,

View File

@@ -106,7 +106,9 @@ export function useDeattachedSender() {
attachmentsMeta.push({ attachmentsMeta.push({
id: attachment.id, id: attachment.id,
type: attachment.type, type: attachment.type,
preview: attachment.preview preview: attachment.preview,
encoding: attachment.encoding,
transport: attachment.transport
}); });
if(attachment.type == AttachmentType.FILE){ if(attachment.type == AttachmentType.FILE){
/** /**
@@ -132,7 +134,7 @@ export function useDeattachedSender() {
|| publicKey == dialog) { || publicKey == dialog) {
return; return;
} }
let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('utf-8'), attachemnts); let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('hex'), attachemnts);
if(attachemnts.length <= 0 && message.trim() == ""){ if(attachemnts.length <= 0 && message.trim() == ""){
runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]); runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]);
updateDialog(dialog); updateDialog(dialog);

View File

@@ -96,14 +96,13 @@ export function useDialog() : {
* же сообщений (смотреть problem_sync.md) * же сообщений (смотреть problem_sync.md)
*/ */
const aesChachaKey = await encodeWithPassword(privatePlain, key.toString('binary')); const aesChachaKey = await encodeWithPassword(privatePlain, key.toString('binary'));
setMessages((prev : Message[]) => ([...prev, { setMessages((prev : Message[]) => ([...prev, {
from_public_key: publicKey, from_public_key: publicKey,
to_public_key: dialog, to_public_key: dialog,
content: content, content: content,
timestamp: Date.now(), timestamp: Date.now(),
readed: publicKey == dialog ? 1 : 0, readed: publicKey == dialog ? 1 : 0,
chacha_key: key.toString('utf-8'), chacha_key: key.toString('hex'),
from_me: 1, from_me: 1,
plain_message: message, plain_message: message,
delivered: publicKey == dialog ? DeliveredMessageState.DELIVERED : DeliveredMessageState.WAITING, delivered: publicKey == dialog ? DeliveredMessageState.DELIVERED : DeliveredMessageState.WAITING,
@@ -147,9 +146,8 @@ export function useDialog() : {
return; return;
} }
//98acbbc68f4b2449daf0a39d1b3eab9a3056da5d45b811bbc903e214c21d39643394980231e1a89c811830d870f3354184319665327ca8bd
console.info("Sending key for message ", key.toString('hex')); console.info("Sending key for message ", key.toString('hex'));
let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('utf-8'), attachemnts); let preparedToNetworkSendAttachements : Attachment[] = await prepareAttachmentsToSend(messageId, dialog, key.toString('hex'), attachemnts);
if(attachemnts.length <= 0 && message.trim() == ""){ if(attachemnts.length <= 0 && message.trim() == ""){
runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]); runQuery("UPDATE messages SET delivered = ? WHERE message_id = ?", [DeliveredMessageState.ERROR, messageId]);
updateDialog(dialog); updateDialog(dialog);

View File

@@ -279,7 +279,7 @@ export function useDialogFiber() {
* Этот тип вложения приходит сразу в blob и не нуждается * Этот тип вложения приходит сразу в blob и не нуждается
* в последующем скачивании * в последующем скачивании
*/ */
const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob); const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob);
writeFile(`m/${await generateMd5(attachment.id + publicKey)}`, writeFile(`m/${await generateMd5(attachment.id + publicKey)}`,
Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary')); Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary'));
messageAttachments[nextLength - 1].blob = decryptedBlob; messageAttachments[nextLength - 1].blob = decryptedBlob;
@@ -300,7 +300,7 @@ export function useDialogFiber() {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: idle ? 0 : 1, readed: idle ? 0 : 1,
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: fromPublicKey == publicKey ? 1 : 0, from_me: fromPublicKey == publicKey ? 1 : 0,
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,

View File

@@ -52,10 +52,7 @@ export function useReplyMessages() {
*/ */
return; return;
} }
message.chacha_key_plain = Buffer.from(message.chacha_key_plain).toString('hex');
replyMessages.messages.push(message);
const sortedByTime = replyMessages.messages.sort((a, b) => a.timestamp - b.timestamp); const sortedByTime = replyMessages.messages.sort((a, b) => a.timestamp - b.timestamp);
setReplyMessages({ setReplyMessages({
publicKey: dialog, publicKey: dialog,
messages: sortedByTime messages: sortedByTime

View File

@@ -181,7 +181,7 @@ export function useSynchronize() {
* Этот тип вложения приходит сразу в blob и не нуждается * Этот тип вложения приходит сразу в blob и не нуждается
* в последующем скачивании * в последующем скачивании
*/ */
const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('utf-8'), attachment.blob); const decryptedBlob = await decodeWithPassword(chachaDecryptedKey.toString('hex'), attachment.blob);
writeFile(`m/${await generateMd5(attachment.id + publicKey)}`, writeFile(`m/${await generateMd5(attachment.id + publicKey)}`,
Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary')); Buffer.from(await encodeWithPassword(privatePlain, decryptedBlob)).toString('binary'));
messageAttachments[nextLength - 1].blob = decryptedBlob; messageAttachments[nextLength - 1].blob = decryptedBlob;
@@ -202,7 +202,7 @@ export function useSynchronize() {
content: content, content: content,
timestamp: timestamp, timestamp: timestamp,
readed: 1, //сообщение прочитано readed: 1, //сообщение прочитано
chacha_key: chachaDecryptedKey.toString('utf-8'), chacha_key: chachaDecryptedKey.toString('hex'),
from_me: 1, //сообщение от нас from_me: 1, //сообщение от нас
plain_message: (decryptedContent as string), plain_message: (decryptedContent as string),
delivered: DeliveredMessageState.DELIVERED, delivered: DeliveredMessageState.DELIVERED,