|
1 | 1 | import UIKit
|
2 | 2 | import Flutter
|
| 3 | +import CryptoSwift |
| 4 | + |
| 5 | +private extension String { |
| 6 | + static let CRYPTO_CHANNEL = "com.hoc.node_auth/crypto" |
| 7 | + static let CRYPTO_ERROR_CODE = "com.hoc.node_auth/crypto_error" |
| 8 | + static let ENCRYPT_METHOD = "encrypt" |
| 9 | + static let DECRYPT_METHOD = "decrypt" |
| 10 | +} |
3 | 11 |
|
4 | 12 | @UIApplicationMain
|
5 | 13 | @objc class AppDelegate: FlutterAppDelegate {
|
6 | 14 | override func application(
|
7 | 15 | _ application: UIApplication,
|
8 | 16 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
9 | 17 | ) -> Bool {
|
| 18 | + let flutterVC = window?.rootViewController as! FlutterViewController |
| 19 | + |
| 20 | + let cryptoChannel = FlutterMethodChannel( |
| 21 | + name: .CRYPTO_CHANNEL, |
| 22 | + binaryMessenger: flutterVC.binaryMessenger |
| 23 | + ) |
| 24 | + cryptoChannel.setMethodCallHandler { call, result in |
| 25 | + switch call.method { |
| 26 | + case .ENCRYPT_METHOD: encrypt(call: call, result: result) |
| 27 | + case .DECRYPT_METHOD: decrypt(call: call, result: result) |
| 28 | + default: |
| 29 | + result(FlutterMethodNotImplemented) |
| 30 | + } |
| 31 | + } |
| 32 | + |
10 | 33 | GeneratedPluginRegistrant.register(with: self)
|
11 | 34 | return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
12 | 35 | }
|
13 | 36 | }
|
| 37 | + |
| 38 | +private enum AESConfig { |
| 39 | + static let iv: [UInt8] = "_hoc081098_auth_".bytes |
| 40 | + static let key: [UInt8] = "__hoc081098_nodejs_auth_rxdart__".bytes |
| 41 | + |
| 42 | + static let backgroundQueue = DispatchQueue.global(qos: .userInitiated) |
| 43 | + |
| 44 | + static func gcm() -> GCM { GCM(iv: AESConfig.iv, mode: .combined) } |
| 45 | +} |
| 46 | + |
| 47 | +private func complete(result: @escaping FlutterResult, with error: Error) { |
| 48 | + NSLog("\n[NODE_AUTH] Error: \(error)") |
| 49 | + |
| 50 | + executeOnMain { |
| 51 | + result( |
| 52 | + FlutterError( |
| 53 | + code: .CRYPTO_ERROR_CODE, |
| 54 | + message: error.localizedDescription, |
| 55 | + details: nil |
| 56 | + ) |
| 57 | + ) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +private func executeOnMain(block: @escaping () -> Void) { |
| 62 | + if Thread.isMainThread { |
| 63 | + block() |
| 64 | + } else { |
| 65 | + DispatchQueue.main.async { |
| 66 | + block() |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +private func useAES( |
| 72 | + input: String, |
| 73 | + result: @escaping FlutterResult, |
| 74 | + inputToBytes: (String) -> [UInt8]?, |
| 75 | + bytesToString: @escaping ([UInt8]) -> String?, |
| 76 | + block: @escaping (AES, [UInt8]) throws -> [UInt8] |
| 77 | +) { |
| 78 | + guard let inputBytes = inputToBytes(input) else { |
| 79 | + NSLog("\n[NODE_AUTH] Error: inputToBytes returns nil") |
| 80 | + |
| 81 | + executeOnMain { |
| 82 | + result( |
| 83 | + FlutterError( |
| 84 | + code: .CRYPTO_ERROR_CODE, |
| 85 | + message: "An unexpected error occurred!", |
| 86 | + details: nil |
| 87 | + ) |
| 88 | + ) |
| 89 | + } |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + AESConfig.backgroundQueue.async { |
| 94 | + let start = DispatchTime.now() |
| 95 | + |
| 96 | + do { |
| 97 | + let aes = try AES( |
| 98 | + key: AESConfig.key, |
| 99 | + blockMode: AESConfig.gcm(), |
| 100 | + padding: .noPadding |
| 101 | + ) |
| 102 | + |
| 103 | + let outputBytes = try block(aes, inputBytes) |
| 104 | + guard let stringResult = bytesToString(outputBytes) else { |
| 105 | + NSLog("\n[NODE_AUTH] Error: bytesToString returns nil") |
| 106 | + |
| 107 | + executeOnMain { |
| 108 | + result( |
| 109 | + FlutterError( |
| 110 | + code: .CRYPTO_ERROR_CODE, |
| 111 | + message: "An unexpected error occurred!", |
| 112 | + details: nil |
| 113 | + ) |
| 114 | + ) |
| 115 | + } |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + let end = DispatchTime.now() |
| 120 | + let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds |
| 121 | + let millisTime = Double(nanoTime) / 1_000_000 |
| 122 | + NSLog("\n[NODE_AUTH] Time: \(millisTime) ms") |
| 123 | + |
| 124 | + executeOnMain { result(stringResult) } |
| 125 | + } catch { |
| 126 | + complete(result: result, with: error) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +private func encrypt(call: FlutterMethodCall, result: @escaping FlutterResult) { |
| 132 | + useAES( |
| 133 | + input: call.arguments as! String, |
| 134 | + result: result, |
| 135 | + inputToBytes: { $0.bytes }, |
| 136 | + bytesToString: base64Encode(bytes:) |
| 137 | + ) { aes, bytes in try aes.encrypt(bytes) } |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +private func decrypt(call: FlutterMethodCall, result: @escaping FlutterResult) { |
| 142 | + useAES( |
| 143 | + input: call.arguments as! String, |
| 144 | + result: result, |
| 145 | + inputToBytes: base64Decode(s:), |
| 146 | + bytesToString: { .init(bytes: $0, encoding: .utf8) } |
| 147 | + ) { aes, bytes in try aes.decrypt(bytes) } |
| 148 | +} |
| 149 | + |
| 150 | +func base64Decode(s: String) -> [UInt8]? { |
| 151 | + Data(base64Encoded: s)?.bytes |
| 152 | +} |
| 153 | + |
| 154 | +func base64Encode(bytes: [UInt8]) -> String { |
| 155 | + Data(bytes).base64EncodedString() |
| 156 | +} |
0 commit comments