Remove unnecessary model
This commit is contained in:
parent
7f707d187e
commit
3f5d20027e
8 changed files with 52 additions and 73 deletions
|
@ -52,24 +52,23 @@ class Configuration {
|
|||
final kek = CryptoUtil.deriveKey(utf8.encode(passphrase), kekSalt);
|
||||
|
||||
// Encrypt the key with this derived key
|
||||
final encryptedKeyData = await CryptoUtil.encrypt(key, key: kek);
|
||||
final encryptedKeyData = await CryptoUtil.encrypt(key, kek);
|
||||
|
||||
// Hash the passphrase so that its correctness can be compared later
|
||||
final kekHash = await CryptoUtil.hash(kek);
|
||||
|
||||
// Generate a public-private keypair and encrypt the latter
|
||||
final keyPair = await CryptoUtil.generateKeyPair();
|
||||
final encryptedSecretKeyData =
|
||||
await CryptoUtil.encrypt(keyPair.sk, key: kek);
|
||||
final encryptedSecretKeyData = await CryptoUtil.encrypt(keyPair.sk, kek);
|
||||
|
||||
final attributes = KeyAttributes(
|
||||
Sodium.bin2base64(kekSalt),
|
||||
kekHash,
|
||||
encryptedKeyData.encryptedData.base64,
|
||||
encryptedKeyData.nonce.base64,
|
||||
Sodium.bin2base64(encryptedKeyData.encryptedData),
|
||||
Sodium.bin2base64(encryptedKeyData.nonce),
|
||||
Sodium.bin2base64(keyPair.pk),
|
||||
encryptedSecretKeyData.encryptedData.base64,
|
||||
encryptedSecretKeyData.nonce.base64,
|
||||
Sodium.bin2base64(encryptedSecretKeyData.encryptedData),
|
||||
Sodium.bin2base64(encryptedSecretKeyData.nonce),
|
||||
);
|
||||
await setKey(Sodium.bin2base64(key));
|
||||
await setKeyAttributes(attributes);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import 'package:photos/models/encryption_attribute.dart';
|
||||
import 'dart:typed_data';
|
||||
|
||||
class EncryptedData {
|
||||
final EncryptionAttribute key;
|
||||
final EncryptionAttribute nonce;
|
||||
final EncryptionAttribute encryptedData;
|
||||
final Uint8List key;
|
||||
final Uint8List nonce;
|
||||
final Uint8List encryptedData;
|
||||
|
||||
EncryptedData(this.key, this.nonce, this.encryptedData);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import 'package:photos/models/encryption_attribute.dart';
|
||||
import 'dart:typed_data';
|
||||
|
||||
class ChaChaAttributes {
|
||||
final EncryptionAttribute key;
|
||||
final EncryptionAttribute header;
|
||||
final Uint8List key;
|
||||
final Uint8List header;
|
||||
|
||||
ChaChaAttributes(this.key, this.header);
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_sodium/flutter_sodium.dart';
|
||||
|
||||
class EncryptionAttribute {
|
||||
String base64;
|
||||
Uint8List bytes;
|
||||
|
||||
EncryptionAttribute({this.base64, this.bytes}) {
|
||||
if (base64 != null) {
|
||||
this.bytes = Sodium.base642bin(base64);
|
||||
} else {
|
||||
this.base64 = Sodium.bin2base64(bytes);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
class ChaChaEncryptionResult {
|
||||
class EncryptionResult {
|
||||
final Uint8List encryptedData;
|
||||
final Uint8List header;
|
||||
final Uint8List nonce;
|
||||
|
||||
ChaChaEncryptionResult(this.encryptedData, {this.header, this.nonce});
|
||||
EncryptionResult(this.encryptedData, {this.header, this.nonce});
|
||||
}
|
|
@ -5,11 +5,10 @@ import 'dart:io' as io;
|
|||
import 'package:computer/computer.dart';
|
||||
import 'package:flutter_sodium/flutter_sodium.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:photos/models/chacha_encryption_result.dart';
|
||||
import 'package:photos/models/encryption_result.dart';
|
||||
|
||||
import 'package:photos/models/encrypted_data_attributes.dart';
|
||||
import 'package:photos/models/encrypted_file_attributes.dart';
|
||||
import 'package:photos/models/encryption_attribute.dart';
|
||||
|
||||
final int encryptionChunkSize = 4 * 1024 * 1024;
|
||||
final int decryptionChunkSize =
|
||||
|
@ -64,8 +63,7 @@ ChaChaAttributes chachaEncryptFile(Map<String, dynamic> args) {
|
|||
logger.info("Encryption time: " +
|
||||
(DateTime.now().millisecondsSinceEpoch - encryptionStartTime).toString());
|
||||
|
||||
return ChaChaAttributes(EncryptionAttribute(bytes: key),
|
||||
EncryptionAttribute(bytes: initPushResult.header));
|
||||
return ChaChaAttributes(key, initPushResult.header);
|
||||
}
|
||||
|
||||
void chachaDecrypt(Map<String, dynamic> args) {
|
||||
|
@ -101,11 +99,7 @@ void chachaDecrypt(Map<String, dynamic> args) {
|
|||
}
|
||||
|
||||
class CryptoUtil {
|
||||
static Future<EncryptedData> encrypt(Uint8List source,
|
||||
{Uint8List key}) async {
|
||||
if (key == null) {
|
||||
key = Sodium.cryptoSecretboxKeygen();
|
||||
}
|
||||
static Future<EncryptedData> encrypt(Uint8List source, Uint8List key) async {
|
||||
final nonce = Sodium.randombytesBuf(Sodium.cryptoSecretboxNoncebytes);
|
||||
|
||||
final args = Map<String, dynamic>();
|
||||
|
@ -113,10 +107,7 @@ class CryptoUtil {
|
|||
args["nonce"] = nonce;
|
||||
args["key"] = key;
|
||||
final encryptedData = cryptoSecretboxEasy(args);
|
||||
return EncryptedData(
|
||||
EncryptionAttribute(bytes: key),
|
||||
EncryptionAttribute(bytes: nonce),
|
||||
EncryptionAttribute(bytes: encryptedData));
|
||||
return EncryptedData(key, nonce, encryptedData);
|
||||
}
|
||||
|
||||
static Future<Uint8List> decrypt(
|
||||
|
@ -133,7 +124,7 @@ class CryptoUtil {
|
|||
}
|
||||
}
|
||||
|
||||
static ChaChaEncryptionResult encryptChaCha(Uint8List source, Uint8List key) {
|
||||
static EncryptionResult encryptChaCha(Uint8List source, Uint8List key) {
|
||||
final initPushResult =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305InitPush(key);
|
||||
final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
|
||||
|
@ -141,7 +132,7 @@ class CryptoUtil {
|
|||
source,
|
||||
null,
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305TagFinal);
|
||||
return ChaChaEncryptionResult(encryptedData, header: initPushResult.header);
|
||||
return EncryptionResult(encryptedData, header: initPushResult.header);
|
||||
}
|
||||
|
||||
static Uint8List decryptChaCha(
|
||||
|
@ -171,8 +162,8 @@ class CryptoUtil {
|
|||
final args = Map<String, dynamic>();
|
||||
args["sourceFilePath"] = sourceFilePath;
|
||||
args["destinationFilePath"] = destinationFilePath;
|
||||
args["header"] = attributes.header.bytes;
|
||||
args["key"] = attributes.key.bytes;
|
||||
args["header"] = attributes.header;
|
||||
args["key"] = attributes.key;
|
||||
return Computer().compute(chachaDecrypt, param: args);
|
||||
}
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ class FileUploader {
|
|||
file.generatedID.toString() + "_thumbnail.encrypted";
|
||||
final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName;
|
||||
final encryptedThumbnail =
|
||||
CryptoUtil.encryptChaCha(thumbnailData, fileAttributes.key.bytes);
|
||||
CryptoUtil.encryptChaCha(thumbnailData, fileAttributes.key);
|
||||
io.File(encryptedThumbnailPath)
|
||||
.writeAsBytesSync(encryptedThumbnail.encryptedData);
|
||||
|
||||
|
@ -73,28 +73,38 @@ class FileUploader {
|
|||
String thumbnailObjectKey =
|
||||
await putFile(thumbnailUploadURL, io.File(encryptedThumbnailPath));
|
||||
|
||||
final encryptedMetadata = CryptoUtil.encryptChaCha(
|
||||
utf8.encode(jsonEncode(file.getMetadata())), fileAttributes.key.bytes);
|
||||
final encryptedMetadataData = CryptoUtil.encryptChaCha(
|
||||
utf8.encode(jsonEncode(file.getMetadata())), fileAttributes.key);
|
||||
|
||||
final encryptedFileKey = await CryptoUtil.encrypt(
|
||||
fileAttributes.key.bytes,
|
||||
key: Configuration.instance.getKey(),
|
||||
final encryptedFileKeyData = await CryptoUtil.encrypt(
|
||||
fileAttributes.key,
|
||||
Configuration.instance.getKey(),
|
||||
);
|
||||
|
||||
final encryptedKey = Sodium.bin2base64(encryptedFileKeyData.encryptedData);
|
||||
final keyDecryptionNonce = Sodium.bin2base64(encryptedFileKeyData.nonce);
|
||||
final fileDecryptionHeader = Sodium.bin2base64(fileAttributes.header);
|
||||
final thumbnailDecryptionHeader =
|
||||
Sodium.bin2base64(encryptedThumbnail.header);
|
||||
final encryptedMetadata =
|
||||
Sodium.bin2base64(encryptedMetadataData.encryptedData);
|
||||
final metadataDecryptionHeader =
|
||||
Sodium.bin2base64(encryptedMetadataData.header);
|
||||
|
||||
final data = {
|
||||
"encryptedKey": encryptedFileKey.encryptedData.base64,
|
||||
"keyDecryptionNonce": encryptedFileKey.nonce.base64,
|
||||
"encryptedKey": encryptedKey,
|
||||
"keyDecryptionNonce": keyDecryptionNonce,
|
||||
"file": {
|
||||
"objectKey": fileObjectKey,
|
||||
"header": fileAttributes.header.base64,
|
||||
"header": fileDecryptionHeader,
|
||||
},
|
||||
"thumbnail": {
|
||||
"objectKey": thumbnailObjectKey,
|
||||
"header": Sodium.bin2base64(encryptedThumbnail.header),
|
||||
"header": thumbnailDecryptionHeader,
|
||||
},
|
||||
"metadata": {
|
||||
"encryptedData": Sodium.bin2base64(encryptedMetadata.encryptedData),
|
||||
"header": Sodium.bin2base64(encryptedMetadata.header),
|
||||
"encryptedData": encryptedMetadata,
|
||||
"header": metadataDecryptionHeader,
|
||||
}
|
||||
};
|
||||
return _dio
|
||||
|
@ -111,13 +121,11 @@ class FileUploader {
|
|||
file.uploadedFileID = data["id"];
|
||||
file.updationTime = data["updationTime"];
|
||||
file.ownerID = data["ownerID"];
|
||||
file.encryptedKey = encryptedFileKey.encryptedData.base64;
|
||||
file.keyDecryptionNonce = encryptedFileKey.nonce.base64;
|
||||
file.fileDecryptionHeader = fileAttributes.header.base64;
|
||||
file.thumbnailDecryptionHeader =
|
||||
Sodium.bin2base64(encryptedThumbnail.header);
|
||||
file.metadataDecryptionHeader =
|
||||
Sodium.bin2base64(encryptedMetadata.header);
|
||||
file.encryptedKey = encryptedKey;
|
||||
file.keyDecryptionNonce = keyDecryptionNonce;
|
||||
file.fileDecryptionHeader = fileDecryptionHeader;
|
||||
file.thumbnailDecryptionHeader = thumbnailDecryptionHeader;
|
||||
file.metadataDecryptionHeader = metadataDecryptionHeader;
|
||||
return file;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ import 'package:photos/core/configuration.dart';
|
|||
import 'package:photos/core/constants.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/models/encrypted_file_attributes.dart';
|
||||
import 'package:photos/models/encryption_attribute.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/models/file_type.dart';
|
||||
|
||||
|
@ -177,10 +176,8 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
|
|||
}
|
||||
logger.info("File downloaded: " + file.uploadedFileID.toString());
|
||||
var attributes = ChaChaAttributes(
|
||||
EncryptionAttribute(
|
||||
bytes: await decryptFileKey(file),
|
||||
),
|
||||
EncryptionAttribute(base64: file.fileDecryptionHeader),
|
||||
await decryptFileKey(file),
|
||||
Sodium.base642bin(file.fileDecryptionHeader),
|
||||
);
|
||||
await CryptoUtil.decryptFile(
|
||||
encryptedFilePath, decryptedFilePath, attributes);
|
||||
|
|
Loading…
Add table
Reference in a new issue