Refactor crypto util
This commit is contained in:
parent
27f019af57
commit
7f707d187e
5 changed files with 27 additions and 26 deletions
|
@ -3,6 +3,7 @@ import 'dart:typed_data';
|
|||
class ChaChaEncryptionResult {
|
||||
final Uint8List encryptedData;
|
||||
final Uint8List header;
|
||||
final Uint8List nonce;
|
||||
|
||||
ChaChaEncryptionResult(this.encryptedData, this.header);
|
||||
}
|
||||
ChaChaEncryptionResult(this.encryptedData, {this.header, this.nonce});
|
||||
}
|
||||
|
|
|
@ -119,26 +119,6 @@ class CryptoUtil {
|
|||
EncryptionAttribute(bytes: encryptedData));
|
||||
}
|
||||
|
||||
static ChaChaEncryptionResult encryptStream(Uint8List source, Uint8List key) {
|
||||
final initPushResult =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305InitPush(key);
|
||||
final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
|
||||
initPushResult.state,
|
||||
source,
|
||||
null,
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305TagFinal);
|
||||
return ChaChaEncryptionResult(encryptedData, initPushResult.header);
|
||||
}
|
||||
|
||||
static Uint8List decryptStream(
|
||||
Uint8List source, Uint8List key, Uint8List header) {
|
||||
final pullState =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305InitPull(header, key);
|
||||
final pullResult =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305Pull(pullState, source, null);
|
||||
return pullResult.m;
|
||||
}
|
||||
|
||||
static Future<Uint8List> decrypt(
|
||||
Uint8List cipher, Uint8List key, Uint8List nonce,
|
||||
{bool background = false}) async {
|
||||
|
@ -153,6 +133,26 @@ class CryptoUtil {
|
|||
}
|
||||
}
|
||||
|
||||
static ChaChaEncryptionResult encryptChaCha(Uint8List source, Uint8List key) {
|
||||
final initPushResult =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305InitPush(key);
|
||||
final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
|
||||
initPushResult.state,
|
||||
source,
|
||||
null,
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305TagFinal);
|
||||
return ChaChaEncryptionResult(encryptedData, header: initPushResult.header);
|
||||
}
|
||||
|
||||
static Uint8List decryptChaCha(
|
||||
Uint8List source, Uint8List key, Uint8List header) {
|
||||
final pullState =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305InitPull(header, key);
|
||||
final pullResult =
|
||||
Sodium.cryptoSecretstreamXchacha20poly1305Pull(pullState, source, null);
|
||||
return pullResult.m;
|
||||
}
|
||||
|
||||
static Future<ChaChaAttributes> encryptFile(
|
||||
String sourceFilePath,
|
||||
String destinationFilePath,
|
||||
|
|
|
@ -42,7 +42,7 @@ class DiffFetcher {
|
|||
file.thumbnailDecryptionHeader =
|
||||
item["thumbnailDecryptionHeader"];
|
||||
file.metadataDecryptionHeader = item["metadataDecryptionHeader"];
|
||||
final encodedMetadata = CryptoUtil.decryptStream(
|
||||
final encodedMetadata = CryptoUtil.decryptChaCha(
|
||||
Sodium.base642bin(item["metadata"]["encryptedData"]),
|
||||
await decryptFileKey(file),
|
||||
Sodium.base642bin(file.metadataDecryptionHeader),
|
||||
|
|
|
@ -65,7 +65,7 @@ class FileUploader {
|
|||
file.generatedID.toString() + "_thumbnail.encrypted";
|
||||
final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName;
|
||||
final encryptedThumbnail =
|
||||
CryptoUtil.encryptStream(thumbnailData, fileAttributes.key.bytes);
|
||||
CryptoUtil.encryptChaCha(thumbnailData, fileAttributes.key.bytes);
|
||||
io.File(encryptedThumbnailPath)
|
||||
.writeAsBytesSync(encryptedThumbnail.encryptedData);
|
||||
|
||||
|
@ -73,7 +73,7 @@ class FileUploader {
|
|||
String thumbnailObjectKey =
|
||||
await putFile(thumbnailUploadURL, io.File(encryptedThumbnailPath));
|
||||
|
||||
final encryptedMetadata = CryptoUtil.encryptStream(
|
||||
final encryptedMetadata = CryptoUtil.encryptChaCha(
|
||||
utf8.encode(jsonEncode(file.getMetadata())), fileAttributes.key.bytes);
|
||||
|
||||
final encryptedFileKey = await CryptoUtil.encrypt(
|
||||
|
|
|
@ -207,7 +207,7 @@ Future<io.File> _downloadAndDecryptThumbnail(File file) async {
|
|||
return Dio().download(file.getThumbnailUrl(), temporaryPath).then((_) async {
|
||||
final encryptedFile = io.File(temporaryPath);
|
||||
final thumbnailDecryptionKey = await decryptFileKey(file);
|
||||
final data = CryptoUtil.decryptStream(
|
||||
final data = CryptoUtil.decryptChaCha(
|
||||
encryptedFile.readAsBytesSync(),
|
||||
thumbnailDecryptionKey,
|
||||
Sodium.base642bin(file.thumbnailDecryptionHeader),
|
||||
|
|
Loading…
Add table
Reference in a new issue