Fix/Update #1
| @ -3,7 +3,6 @@ | ||||
|    <application | ||||
|         android:label="native_crypto_example" | ||||
|         android:name="${applicationName}" | ||||
|         android:largeHeap="true" | ||||
|         android:icon="@mipmap/ic_launcher"> | ||||
|         <activity | ||||
|             android:name=".MainActivity" | ||||
|  | ||||
| @ -3,7 +3,7 @@ | ||||
| // ----- | ||||
| // File: benchmark_page.dart | ||||
| // Created Date: 28/12/2021 15:12:39 | ||||
| // Last Modified: 28/12/2021 15:21:05 | ||||
| // Last Modified: 28/12/2021 17:01:41 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| @ -23,6 +23,34 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|   final Output keyContent = Output(); | ||||
|   final Output benchmarkStatus = Output(large: true); | ||||
| 
 | ||||
|   Future<void> _test(WidgetRef ref, Cipher cipher) async { | ||||
|     Session state = ref.read(sessionProvider.state).state; | ||||
| 
 | ||||
|     if (state.secretKey.bytes.isEmpty) { | ||||
|       benchmarkStatus | ||||
|           .print('No SecretKey!\nGo in Key tab and generate or derive one.'); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     int size = 64; | ||||
|     benchmarkStatus.print("Benchmark Test\n"); | ||||
|      | ||||
|     // Encryption | ||||
|     var before = DateTime.now(); | ||||
|     var encryptedBigFile = await cipher.encrypt(Uint8List(size * 1000000)); | ||||
|     var after = DateTime.now(); | ||||
|     var benchmark = | ||||
|           after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; | ||||
|       benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n'); | ||||
|      | ||||
|     // Decryption | ||||
|     var befored = DateTime.now(); | ||||
|     await cipher.decrypt(encryptedBigFile); | ||||
|     var afterd = DateTime.now(); | ||||
|     var benchmarkd = afterd.millisecondsSinceEpoch - befored.millisecondsSinceEpoch; | ||||
|       benchmarkStatus.append('[$size MB] Decryption took $benchmarkd ms\n'); | ||||
|   } | ||||
| 
 | ||||
|   Future<void> _benchmark(WidgetRef ref, Cipher cipher) async { | ||||
|     Session state = ref.read(sessionProvider.state).state; | ||||
| 
 | ||||
| @ -39,13 +67,14 @@ class BenchmarkPage extends ConsumerWidget { | ||||
| 
 | ||||
|     var beforeBench = DateTime.now(); | ||||
|     for (int size in testedSizes) { | ||||
|       var bigFile = Uint8List(size * 1000000); | ||||
|       var b = ByteData(size * 1000000); | ||||
|       //var bigFile = Uint8List.view(); | ||||
|       csv += "${size * 1000000};"; | ||||
|       var cryptoTime = 0; | ||||
| 
 | ||||
|       // Encryption | ||||
|       var before = DateTime.now(); | ||||
|       var encryptedBigFile = await cipher.encrypt(bigFile); | ||||
|       var encryptedBigFile = await cipher.encrypt(b.buffer.asUint8List()); | ||||
|       var after = DateTime.now(); | ||||
| 
 | ||||
|       var benchmark = | ||||
| @ -122,6 +151,10 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|                   () => _benchmark(ref, cipher), | ||||
|                   "Launch benchmark", | ||||
|                 ), | ||||
|                 Button( | ||||
|                   () => _test(ref, cipher), | ||||
|                   "Test benchmark", | ||||
|                 ), | ||||
|                 Button( | ||||
|                   _clear, | ||||
|                   "Clear", | ||||
|  | ||||
| @ -18,53 +18,85 @@ class NativeCryptoAndroidPlugin: FlutterPlugin, MethodCallHandler { | ||||
|   private lateinit var channel : MethodChannel | ||||
| 
 | ||||
|   override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||||
|     channel = MethodChannel(flutterPluginBinding.binaryMessenger, "plugins.hugop.cl/native_crypto") | ||||
|     channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "plugins.hugop.cl/native_crypto") | ||||
|     channel.setMethodCallHandler(this) | ||||
|   } | ||||
| 
 | ||||
|   override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||||
|     when (call.method) { | ||||
|       "digest" -> { | ||||
|         val data : ByteArray? = call.argument<ByteArray>("data") | ||||
|         val algorithm : String? = call.argument<String>("algorithm") | ||||
|         // TODO(hpcl): check if algorithm is null | ||||
|         // TODO(hpcl): check if digest is null | ||||
|         result.success(Hash.digest(data, algorithm!!)) | ||||
|         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||
|         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||
| 
 | ||||
|         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||
|         val algorithm : String = call.argument<String>("algorithm")!! | ||||
| 
 | ||||
|         result.success(Hash.digest(data, algorithm)) | ||||
|       } | ||||
|       "generateSecretKey" -> { | ||||
|         val bitsCount : Int? = call.argument<Int>("bitsCount") | ||||
|         // TODO(hpcl): check null | ||||
|         result.success(Key.fromSecureRandom(bitsCount!!)) | ||||
|         if (!call.hasArgument("bitsCount")) result.error("SIZE_NULL", null, null); | ||||
| 
 | ||||
|         val bitsCount : Int = call.argument<Int>("bitsCount")!! | ||||
| 
 | ||||
|         result.success(Key.fromSecureRandom(bitsCount)) | ||||
|       } | ||||
|       "generateKeyPair" -> { | ||||
|         result.notImplemented() | ||||
|       } | ||||
|       "pbkdf2" -> { | ||||
|         val password : String? = call.argument<String>("password") | ||||
|         val salt : String? = call.argument<String>("salt") | ||||
|         val keyBytesCount : Int? = call.argument<Int>("keyBytesCount") | ||||
|         val iterations : Int? = call.argument<Int>("iterations") | ||||
|         val algorithm : String? = call.argument<String>("algorithm") | ||||
|         // TODO(hpcl): check null | ||||
|         result.success(Key.fromPBKDF2(password!!, salt!!, keyBytesCount!!, iterations!!, algorithm!!)) | ||||
|         if (!call.hasArgument("password")) result.error("PASSWORD_NULL", null, null); | ||||
|         if (!call.hasArgument("salt")) result.error("SALT_NULL", null, null); | ||||
|         if (!call.hasArgument("keyBytesCount")) result.error("SIZE_NULL", null, null); | ||||
|         if (!call.hasArgument("iterations")) result.error("ITERATIONS_NULL", null, null); | ||||
|         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||
| 
 | ||||
|         val password : String = call.argument<String>("password")!! | ||||
|         val salt : String = call.argument<String>("salt")!! | ||||
|         val keyBytesCount : Int = call.argument<Int>("keyBytesCount")!! | ||||
|         val iterations : Int = call.argument<Int>("iterations")!! | ||||
|         val algorithm : String = call.argument<String>("algorithm")!! | ||||
| 
 | ||||
|         result.success(Key.fromPBKDF2(password, salt, keyBytesCount, iterations, algorithm)) | ||||
|       } | ||||
|       "encrypt" -> { | ||||
|         val data : ByteArray? = call.argument<ByteArray>("data") | ||||
|         val key : ByteArray? = call.argument<ByteArray>("key") | ||||
|         val algorithm : String? = call.argument<String>("algorithm") | ||||
|         // TODO(hpcl): check null | ||||
|         // TODO(hcpl): check algorithm | ||||
|         result.success(AESCipher().encrypt(data!!, key!!)) | ||||
|         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||
|         if (!call.hasArgument("key")) result.error("KEY_NULL", null, null); | ||||
|         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||
| 
 | ||||
|         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||
|         val key : ByteArray = call.argument<ByteArray>("key")!! | ||||
|         val algorithm : String = call.argument<String>("algorithm")!! | ||||
| 
 | ||||
|         if (algorithm == "aes") { | ||||
|           result.success(AESCipher().encrypt(data, key)) | ||||
|         } | ||||
|       } | ||||
|       "decrypt" ->  { | ||||
|         val data : ByteArray? = call.argument<ByteArray>("data") | ||||
|         val key : ByteArray? = call.argument<ByteArray>("key") | ||||
|         val algorithm : String? = call.argument<String>("algorithm") | ||||
|         // TODO(hpcl): check null | ||||
|         // TODO(hcpl): check algorithm | ||||
|         result.success(AESCipher().decrypt(data!!, key!!)) | ||||
|         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||
|         if (!call.hasArgument("key")) result.error("KEY_NULL", null, null); | ||||
|         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||
| 
 | ||||
|         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||
|         val key : ByteArray = call.argument<ByteArray>("key")!! | ||||
|         val algorithm : String = call.argument<String>("algorithm")!! | ||||
| 
 | ||||
|         if (algorithm == "aes") { | ||||
|           result.success(AESCipher().decrypt(data, key)) | ||||
|         } | ||||
|       } | ||||
|       "generateSharedSecretKey" -> { | ||||
|         if (!call.hasArgument("salt")) result.error("SALT_NULL", null, null); | ||||
|         if (!call.hasArgument("keyBytesCount")) result.error("SIZE_NULL", null, null); | ||||
|         if (!call.hasArgument("ephemeralPrivateKey")) result.error("PRIVATE_KEY_NULL", null, null); | ||||
|         if (!call.hasArgument("otherPublicKey")) result.error("PUBLIC_KEY_NULL", null, null); | ||||
|         if (!call.hasArgument("hkdfAlgorithm")) result.error("ALGORITHM_NULL", null, null); | ||||
| 
 | ||||
|         val salt : ByteArray = call.argument<ByteArray>("salt")!! | ||||
|         val keyBytesCount : Int = call.argument<Int>("keyBytesCount")!! | ||||
|         val ephemeralPrivateKey : ByteArray = call.argument<ByteArray>("ephemeralPrivateKey")!! | ||||
|         val otherPublicKey : ByteArray = call.argument<ByteArray>("otherPublicKey")!! | ||||
|         val hkdfAlgorithm : String = call.argument<String>("hkdfAlgorithm")!! | ||||
| 
 | ||||
|         result.notImplemented() | ||||
|       } | ||||
|       else -> result.notImplemented() | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user