Android file encryption

This commit is contained in:
Diego Mello 2024-04-30 09:05:56 -03:00
parent 77d32f4747
commit 5270163e3c
4 changed files with 4043 additions and 37 deletions

View File

@ -43,6 +43,7 @@ class Encryption {
handshake: Function;
decrypt: Function;
encrypt: Function;
encryptFile: Function;
encryptUpload: Function;
importRoomKey: Function;
};
@ -514,6 +515,26 @@ class Encryption {
// Decrypt multiple subscriptions
decryptSubscriptions = (subscriptions: ISubscription[]) => Promise.all(subscriptions.map(s => this.decryptSubscription(s)));
// Encrypt a file
encryptFile = async (rid: string, path: string) => {
try {
// If the client is not ready
if (!this.ready) {
// Wait for ready status
await this.establishing;
}
const roomE2E = await this.getRoomInstance(rid);
return roomE2E.encryptFile(path);
} catch (e) {
console.error(e);
}
// Send a non encrypted message
return null;
};
}
const encryption = new Encryption();

View File

@ -269,6 +269,33 @@ export default class EncryptionRoom {
return message;
};
// Encrypt
encryptFile = async (p: string) => {
console.log('🚀 ~ EncryptionRoom ~ encryptFile= ~ p:', p);
if (!this.ready) {
console.log('🚀 ~ EncryptionRoom ~ encryptFile= ~ this.ready:', this.ready);
return null;
}
try {
// const path = utf8ToBuffer(p);
const path = p;
console.log('🚀 ~ EncryptionRoom ~ encryptFile= ~ path:', path);
const vector = await SimpleCrypto.utils.randomBytes(16);
console.log('🚀 ~ EncryptionRoom ~ encryptFile= ~ vector:', vector);
const data = await SimpleCrypto.AES.encryptFile(path, this.roomKey as ArrayBuffer, vector);
console.log('🚀 ~ EncryptionRoom ~ encryptFile= ~ data:', data);
// return this.keyID + bufferToB64(joinVectorData(vector, data));
return data;
} catch (e) {
// Do nothing
console.error(e);
}
return null;
};
// Decrypt text
decryptText = async (msg: string | ArrayBuffer) => {
if (!msg) {

View File

@ -35,6 +35,7 @@ import {
import { sendFileMessage, sendMessage } from '../../lib/methods';
import { hasPermission, isAndroid, canUploadFile, isReadOnly, isBlocked } from '../../lib/methods/helpers';
import { RoomContext } from '../RoomView/context';
import { Encryption } from '../../lib/encryption';
interface IShareViewState {
selected: IShareAttachment;
@ -249,43 +250,52 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
}
try {
// Send attachment
if (attachments.length) {
await Promise.all(
attachments.map(({ filename: name, mime: type, description, size, path, canUpload }) => {
if (canUpload) {
return sendFileMessage(
room.rid,
{
rid: room.rid,
name,
description,
size,
type,
path,
store: 'Uploads',
msg
},
thread?.id,
server,
{ id: user.id, token: user.token }
);
}
return Promise.resolve();
})
);
// Send text message
} else if (text.length) {
await sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser);
}
} catch {
if (!this.isShareExtension) {
const text = this.messageComposerRef.current?.getText();
this.finishShareView(text, this.state.selectedMessages);
}
console.log(attachments[0].path);
const encryptedFile = await Encryption.encryptFile(room.rid, attachments[0].path);
console.log('🚀 ~ ShareView ~ attachments.map ~ encryptedFile:', encryptedFile);
} catch (e) {
console.error(e);
}
// try {
// // Send attachment
// if (attachments.length) {
// console.log('🚀 ~ ShareView ~ send= ~ attachments:', attachments);
// await Promise.all(
// attachments.map(async ({ filename: name, mime: type, description, size, path, canUpload }) => {
// if (canUpload) {
// return sendFileMessage(
// room.rid,
// {
// rid: room.rid,
// name,
// description,
// size,
// type,
// path,
// store: 'Uploads',
// msg
// },
// thread?.id,
// server,
// { id: user.id, token: user.token }
// );
// }
// return Promise.resolve();
// })
// );
// // Send text message
// } else if (text.length) {
// await sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser);
// }
// } catch {
// if (!this.isShareExtension) {
// const text = this.messageComposerRef.current?.getText();
// this.finishShareView(text, this.state.selectedMessages);
// }
// }
// if it's share extension this should close
if (this.isShareExtension) {
sendLoadingEvent({ visible: false });
@ -351,8 +361,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
selectedMessages,
onSendMessage: this.send,
onRemoveQuoteMessage: this.onRemoveQuoteMessage
}}
>
}}>
<View style={styles.container}>
<Preview
// using key just to reset zoom/move after change selected

File diff suppressed because it is too large Load Diff