Android file encryption
This commit is contained in:
parent
77d32f4747
commit
5270163e3c
|
@ -43,6 +43,7 @@ class Encryption {
|
||||||
handshake: Function;
|
handshake: Function;
|
||||||
decrypt: Function;
|
decrypt: Function;
|
||||||
encrypt: Function;
|
encrypt: Function;
|
||||||
|
encryptFile: Function;
|
||||||
encryptUpload: Function;
|
encryptUpload: Function;
|
||||||
importRoomKey: Function;
|
importRoomKey: Function;
|
||||||
};
|
};
|
||||||
|
@ -514,6 +515,26 @@ class Encryption {
|
||||||
|
|
||||||
// Decrypt multiple subscriptions
|
// Decrypt multiple subscriptions
|
||||||
decryptSubscriptions = (subscriptions: ISubscription[]) => Promise.all(subscriptions.map(s => this.decryptSubscription(s)));
|
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();
|
const encryption = new Encryption();
|
||||||
|
|
|
@ -269,6 +269,33 @@ export default class EncryptionRoom {
|
||||||
return message;
|
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
|
// Decrypt text
|
||||||
decryptText = async (msg: string | ArrayBuffer) => {
|
decryptText = async (msg: string | ArrayBuffer) => {
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
|
|
|
@ -35,6 +35,7 @@ import {
|
||||||
import { sendFileMessage, sendMessage } from '../../lib/methods';
|
import { sendFileMessage, sendMessage } from '../../lib/methods';
|
||||||
import { hasPermission, isAndroid, canUploadFile, isReadOnly, isBlocked } from '../../lib/methods/helpers';
|
import { hasPermission, isAndroid, canUploadFile, isReadOnly, isBlocked } from '../../lib/methods/helpers';
|
||||||
import { RoomContext } from '../RoomView/context';
|
import { RoomContext } from '../RoomView/context';
|
||||||
|
import { Encryption } from '../../lib/encryption';
|
||||||
|
|
||||||
interface IShareViewState {
|
interface IShareViewState {
|
||||||
selected: IShareAttachment;
|
selected: IShareAttachment;
|
||||||
|
@ -249,42 +250,51 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send attachment
|
console.log(attachments[0].path);
|
||||||
if (attachments.length) {
|
const encryptedFile = await Encryption.encryptFile(room.rid, attachments[0].path);
|
||||||
await Promise.all(
|
console.log('🚀 ~ ShareView ~ attachments.map ~ encryptedFile:', encryptedFile);
|
||||||
attachments.map(({ filename: name, mime: type, description, size, path, canUpload }) => {
|
} catch (e) {
|
||||||
if (canUpload) {
|
console.error(e);
|
||||||
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
|
// try {
|
||||||
} else if (text.length) {
|
// // Send attachment
|
||||||
await sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser);
|
// if (attachments.length) {
|
||||||
}
|
// console.log('🚀 ~ ShareView ~ send= ~ attachments:', attachments);
|
||||||
} catch {
|
// await Promise.all(
|
||||||
if (!this.isShareExtension) {
|
// attachments.map(async ({ filename: name, mime: type, description, size, path, canUpload }) => {
|
||||||
const text = this.messageComposerRef.current?.getText();
|
// if (canUpload) {
|
||||||
this.finishShareView(text, this.state.selectedMessages);
|
// 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 it's share extension this should close
|
||||||
if (this.isShareExtension) {
|
if (this.isShareExtension) {
|
||||||
|
@ -351,8 +361,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
|
||||||
selectedMessages,
|
selectedMessages,
|
||||||
onSendMessage: this.send,
|
onSendMessage: this.send,
|
||||||
onRemoveQuoteMessage: this.onRemoveQuoteMessage
|
onRemoveQuoteMessage: this.onRemoveQuoteMessage
|
||||||
}}
|
}}>
|
||||||
>
|
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Preview
|
<Preview
|
||||||
// using key just to reset zoom/move after change selected
|
// using key just to reset zoom/move after change selected
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue