Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev'
Browse files Browse the repository at this point in the history
# Conflicts:
#	benchmark/crypt_benchmark.dart
#	benchmark/models.dart
  • Loading branch information
xaldarof committed Aug 4, 2024
2 parents d4bfba2 + 9a33dfe commit 5919799
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = 24
targetSdk = flutter.targetSdkVersion
versionCode = 4
versionCode = 5
versionName = "1.0.3"
}

Expand Down
58 changes: 37 additions & 21 deletions benchmark/crypt_benchmark.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:safe_file_sender/benchmark/models.dart';
import 'package:safe_file_sender/crypto/crypto.dart';

final _file = File("${Directory.current.path}/files/input.exe");
late Uint8List _testKey;

void main() {
final start = DateTime.now().millisecondsSinceEpoch;
final userA = AppCrypto.generateECKeyPair();
final userB = AppCrypto.generateECKeyPair();
final sharedKey =
AppCrypto.deriveSharedSecret(userA.privateKey, userB.publicKey);

final file = File("${Directory.current.path}/files/input.exe");
final endDeriveSharedKey = DateTime.now().millisecondsSinceEpoch;
print("Benchmark derive shared key: ${(endDeriveSharedKey - start)}ms");
final hmac = AppCrypto.generateHMAC(sharedKey, file.readAsBytesSync());
print("HMAC ${hex.encode(hmac)}");
final end = DateTime.now().millisecondsSinceEpoch;
print("Benchmark generate HMAC: ${(end - start)}ms");

///Benchmark encryption
final startEnc = DateTime.now().millisecondsSinceEpoch;
AppCrypto.encryptAES(file.readAsBytesSync(), sharedKey);
final endEnc = DateTime.now().millisecondsSinceEpoch;

print("Benchmark encryption: ${(endEnc - startEnc)}ms");
_testKey = base64Decode(
AppCrypto.encodeECPublicKey(AppCrypto.generateECKeyPair().publicKey));
_benchmarkSharedKey();
_benchmarkEncryption();
_benchmarkHmacGeneration();
}

void _benchmarkSharedKey() {
final benchmark = BenchmarkTimer("share key derive");

benchmark.run(() {
final userA = AppCrypto.generateECKeyPair();
final userB = AppCrypto.generateECKeyPair();
AppCrypto.deriveSharedSecret(userA.privateKey, userB.publicKey);
});
}

void _benchmarkHmacGeneration() {
final benchmark = BenchmarkTimer("hmac generation");

benchmark.run(() {
AppCrypto.generateHMAC(_testKey, _file.readAsBytesSync());
});
}

void _benchmarkEncryption() {
final benchmark = BenchmarkTimer("file encryption");
benchmark.run(() {
AppCrypto.encryptAES(_file.readAsBytesSync(), _testKey);
});
}

14 changes: 14 additions & 0 deletions benchmark/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class BenchmarkTimer {
final String _name;

int run(Function() action) {
final start = DateTime.now().millisecondsSinceEpoch;
action.call();
final end = DateTime.now().millisecondsSinceEpoch;
final result = end - start;
print("Benchmark for ($_name): $result ms");
return result;
}

BenchmarkTimer(this._name);
}
17 changes: 2 additions & 15 deletions lib/io/socket_client.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:background_downloader/background_downloader.dart';
import 'package:dio/dio.dart';
import 'package:safe_file_sender/dev/logger.dart';
import 'package:safe_file_sender/io/downloader.dart';
import 'package:safe_file_sender/models/base/base_event_listener.dart';
import 'package:safe_file_sender/models/event_listeners.dart';
import 'package:safe_file_sender/utils/file_utils.dart';
import 'package:signalr_netcore/json_hub_protocol.dart';
import 'package:signalr_netcore/signalr_client.dart';
Expand Down Expand Up @@ -147,17 +148,3 @@ class ConnectionClient {
}
}
}

abstract class BaseEventListeners {
Future<void> onPublicKeyReceived(String publicKey);

Future<void> onConnected();
}

abstract class ReceiverListeners extends BaseEventListeners {
Future<void> onIdentifierReceived(String publicKey);

Future<void> onFileReceived(String fileId, String fileName, String hmac);
}

abstract class SenderListeners extends BaseEventListeners {}
5 changes: 5 additions & 0 deletions lib/models/base/base_event_listener.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
abstract class BaseEventListeners {
Future<void> onPublicKeyReceived(String publicKey);

Future<void> onConnected();
}
9 changes: 9 additions & 0 deletions lib/models/event_listeners.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:safe_file_sender/models/base/base_event_listener.dart';

abstract class ReceiverListeners extends BaseEventListeners {
Future<void> onIdentifierReceived(String publicKey);

Future<void> onFileReceived(String fileId, String fileName, String hmac);
}

abstract class SenderListeners extends BaseEventListeners {}
1 change: 1 addition & 0 deletions lib/receive_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pointycastle/ecc/api.dart';
import 'package:safe_file_sender/io/socket_client.dart';
import 'package:safe_file_sender/models/event_listeners.dart';
import 'package:safe_file_sender/models/state_controller.dart';
import 'package:safe_file_sender/utils/file_utils.dart';
import 'package:safe_file_sender/utils/string_utils.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/send_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:safe_file_sender/crypto/crypto.dart';
import 'package:safe_file_sender/dev/logger.dart';
import 'package:safe_file_sender/io/socket_client.dart';
import 'package:safe_file_sender/models/event_listeners.dart';
import 'package:safe_file_sender/models/state_controller.dart';
import 'package:safe_file_sender/utils/file_utils.dart';
import 'package:safe_file_sender/utils/string_utils.dart';
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/file_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io';

import 'package:safe_file_sender/dev/logger.dart';

class FileUtils {
static String fileName(String path) =>
path.split(Platform.pathSeparator).last;
Expand All @@ -9,8 +11,10 @@ extension FileExt on File {
bool safeDelete() {
try {
delete();
logMessage("File : $path deleted!");
return true;
} catch (e) {
logMessage("Delete $path error");
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.3+3
version: 1.0.3

environment:
sdk: '>=3.4.0 <4.0.0'
Expand Down

0 comments on commit 5919799

Please sign in to comment.