Fix/Update #1
| @ -3,7 +3,6 @@ | |||||||
|    <application |    <application | ||||||
|         android:label="native_crypto_example" |         android:label="native_crypto_example" | ||||||
|         android:name="${applicationName}" |         android:name="${applicationName}" | ||||||
|         android:largeHeap="true" |  | ||||||
|         android:icon="@mipmap/ic_launcher"> |         android:icon="@mipmap/ic_launcher"> | ||||||
|         <activity |         <activity | ||||||
|             android:name=".MainActivity" |             android:name=".MainActivity" | ||||||
|  | |||||||
| @ -3,7 +3,7 @@ | |||||||
| // ----- | // ----- | ||||||
| // File: benchmark_page.dart | // File: benchmark_page.dart | ||||||
| // Created Date: 28/12/2021 15:12:39 | // 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 | // Copyright (c) 2021 | ||||||
| 
 | 
 | ||||||
| @ -23,6 +23,34 @@ class BenchmarkPage extends ConsumerWidget { | |||||||
|   final Output keyContent = Output(); |   final Output keyContent = Output(); | ||||||
|   final Output benchmarkStatus = Output(large: true); |   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 { |   Future<void> _benchmark(WidgetRef ref, Cipher cipher) async { | ||||||
|     Session state = ref.read(sessionProvider.state).state; |     Session state = ref.read(sessionProvider.state).state; | ||||||
| 
 | 
 | ||||||
| @ -39,13 +67,14 @@ class BenchmarkPage extends ConsumerWidget { | |||||||
| 
 | 
 | ||||||
|     var beforeBench = DateTime.now(); |     var beforeBench = DateTime.now(); | ||||||
|     for (int size in testedSizes) { |     for (int size in testedSizes) { | ||||||
|       var bigFile = Uint8List(size * 1000000); |       var b = ByteData(size * 1000000); | ||||||
|  |       //var bigFile = Uint8List.view(); | ||||||
|       csv += "${size * 1000000};"; |       csv += "${size * 1000000};"; | ||||||
|       var cryptoTime = 0; |       var cryptoTime = 0; | ||||||
| 
 | 
 | ||||||
|       // Encryption |       // Encryption | ||||||
|       var before = DateTime.now(); |       var before = DateTime.now(); | ||||||
|       var encryptedBigFile = await cipher.encrypt(bigFile); |       var encryptedBigFile = await cipher.encrypt(b.buffer.asUint8List()); | ||||||
|       var after = DateTime.now(); |       var after = DateTime.now(); | ||||||
| 
 | 
 | ||||||
|       var benchmark = |       var benchmark = | ||||||
| @ -122,6 +151,10 @@ class BenchmarkPage extends ConsumerWidget { | |||||||
|                   () => _benchmark(ref, cipher), |                   () => _benchmark(ref, cipher), | ||||||
|                   "Launch benchmark", |                   "Launch benchmark", | ||||||
|                 ), |                 ), | ||||||
|  |                 Button( | ||||||
|  |                   () => _test(ref, cipher), | ||||||
|  |                   "Test benchmark", | ||||||
|  |                 ), | ||||||
|                 Button( |                 Button( | ||||||
|                   _clear, |                   _clear, | ||||||
|                   "Clear", |                   "Clear", | ||||||
|  | |||||||
| @ -18,53 +18,85 @@ class NativeCryptoAndroidPlugin: FlutterPlugin, MethodCallHandler { | |||||||
|   private lateinit var channel : MethodChannel |   private lateinit var channel : MethodChannel | ||||||
| 
 | 
 | ||||||
|   override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |   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) |     channel.setMethodCallHandler(this) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { |   override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||||||
|     when (call.method) { |     when (call.method) { | ||||||
|       "digest" -> { |       "digest" -> { | ||||||
|         val data : ByteArray? = call.argument<ByteArray>("data") |         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||||
|         val algorithm : String? = call.argument<String>("algorithm") |         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||||
|         // TODO(hpcl): check if algorithm is null | 
 | ||||||
|         // TODO(hpcl): check if digest is null |         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||||
|         result.success(Hash.digest(data, algorithm!!)) |         val algorithm : String = call.argument<String>("algorithm")!! | ||||||
|  | 
 | ||||||
|  |         result.success(Hash.digest(data, algorithm)) | ||||||
|       } |       } | ||||||
|       "generateSecretKey" -> { |       "generateSecretKey" -> { | ||||||
|         val bitsCount : Int? = call.argument<Int>("bitsCount") |         if (!call.hasArgument("bitsCount")) result.error("SIZE_NULL", null, null); | ||||||
|         // TODO(hpcl): check null | 
 | ||||||
|         result.success(Key.fromSecureRandom(bitsCount!!)) |         val bitsCount : Int = call.argument<Int>("bitsCount")!! | ||||||
|  | 
 | ||||||
|  |         result.success(Key.fromSecureRandom(bitsCount)) | ||||||
|       } |       } | ||||||
|       "generateKeyPair" -> { |       "generateKeyPair" -> { | ||||||
|         result.notImplemented() |         result.notImplemented() | ||||||
|       } |       } | ||||||
|       "pbkdf2" -> { |       "pbkdf2" -> { | ||||||
|         val password : String? = call.argument<String>("password") |         if (!call.hasArgument("password")) result.error("PASSWORD_NULL", null, null); | ||||||
|         val salt : String? = call.argument<String>("salt") |         if (!call.hasArgument("salt")) result.error("SALT_NULL", null, null); | ||||||
|         val keyBytesCount : Int? = call.argument<Int>("keyBytesCount") |         if (!call.hasArgument("keyBytesCount")) result.error("SIZE_NULL", null, null); | ||||||
|         val iterations : Int? = call.argument<Int>("iterations") |         if (!call.hasArgument("iterations")) result.error("ITERATIONS_NULL", null, null); | ||||||
|         val algorithm : String? = call.argument<String>("algorithm") |         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||||
|         // TODO(hpcl): check null | 
 | ||||||
|         result.success(Key.fromPBKDF2(password!!, salt!!, keyBytesCount!!, iterations!!, algorithm!!)) |         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" -> { |       "encrypt" -> { | ||||||
|         val data : ByteArray? = call.argument<ByteArray>("data") |         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||||
|         val key : ByteArray? = call.argument<ByteArray>("key") |         if (!call.hasArgument("key")) result.error("KEY_NULL", null, null); | ||||||
|         val algorithm : String? = call.argument<String>("algorithm") |         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||||
|         // TODO(hpcl): check null | 
 | ||||||
|         // TODO(hcpl): check algorithm |         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||||
|         result.success(AESCipher().encrypt(data!!, key!!)) |         val key : ByteArray = call.argument<ByteArray>("key")!! | ||||||
|  |         val algorithm : String = call.argument<String>("algorithm")!! | ||||||
|  | 
 | ||||||
|  |         if (algorithm == "aes") { | ||||||
|  |           result.success(AESCipher().encrypt(data, key)) | ||||||
|  |         } | ||||||
|       } |       } | ||||||
|       "decrypt" ->  { |       "decrypt" ->  { | ||||||
|         val data : ByteArray? = call.argument<ByteArray>("data") |         if (!call.hasArgument("data")) result.error("DATA_NULL", null, null); | ||||||
|         val key : ByteArray? = call.argument<ByteArray>("key") |         if (!call.hasArgument("key")) result.error("KEY_NULL", null, null); | ||||||
|         val algorithm : String? = call.argument<String>("algorithm") |         if (!call.hasArgument("algorithm")) result.error("ALGORITHM_NULL", null, null); | ||||||
|         // TODO(hpcl): check null | 
 | ||||||
|         // TODO(hcpl): check algorithm |         val data : ByteArray = call.argument<ByteArray>("data")!! | ||||||
|         result.success(AESCipher().decrypt(data!!, key!!)) |         val key : ByteArray = call.argument<ByteArray>("key")!! | ||||||
|  |         val algorithm : String = call.argument<String>("algorithm")!! | ||||||
|  | 
 | ||||||
|  |         if (algorithm == "aes") { | ||||||
|  |           result.success(AESCipher().decrypt(data, key)) | ||||||
|  |         } | ||||||
|       } |       } | ||||||
|       "generateSharedSecretKey" -> { |       "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() |         result.notImplemented() | ||||||
|       } |       } | ||||||
|       else -> result.notImplemented() |       else -> result.notImplemented() | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user