Add swift key size support
This commit is contained in:
parent
c79c4e5097
commit
6cbe00a609
@ -1,7 +1,6 @@
|
|||||||
import Flutter
|
import Flutter
|
||||||
import UIKit
|
import UIKit
|
||||||
import CryptoKit
|
import CryptoKit
|
||||||
import Foundation
|
|
||||||
import CommonCrypto
|
import CommonCrypto
|
||||||
|
|
||||||
extension FlutterStandardTypedData {
|
extension FlutterStandardTypedData {
|
||||||
@ -99,8 +98,10 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
|
|||||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||||
switch call.method {
|
switch call.method {
|
||||||
case "symKeygen":
|
case "symKeygen":
|
||||||
|
let args = call.arguments as! NSDictionary
|
||||||
|
let keySize = args["size"] as! NSNumber
|
||||||
|
|
||||||
let keyBytes = symKeygen()
|
let keyBytes = symKeygen(keySize: keySize)!
|
||||||
|
|
||||||
result(FlutterStandardTypedData.init(bytes: keyBytes))
|
result(FlutterStandardTypedData.init(bytes: keyBytes))
|
||||||
|
|
||||||
@ -108,6 +109,7 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
|
|||||||
let args = call.arguments as! NSDictionary
|
let args = call.arguments as! NSDictionary
|
||||||
let payload = (args["payload"] as! FlutterStandardTypedData).data
|
let payload = (args["payload"] as! FlutterStandardTypedData).data
|
||||||
let aesKey = (args["aesKey"] as! FlutterStandardTypedData).data
|
let aesKey = (args["aesKey"] as! FlutterStandardTypedData).data
|
||||||
|
|
||||||
let encryptedPayloadIV = symEncrypt(payload: payload, aesKey: aesKey)
|
let encryptedPayloadIV = symEncrypt(payload: payload, aesKey: aesKey)
|
||||||
|
|
||||||
result(encryptedPayloadIV)
|
result(encryptedPayloadIV)
|
||||||
@ -121,6 +123,7 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
|
|||||||
let encryptedPayload = [encrypted, iv]
|
let encryptedPayload = [encrypted, iv]
|
||||||
|
|
||||||
let aesKey = (args["aesKey"] as! FlutterStandardTypedData).data
|
let aesKey = (args["aesKey"] as! FlutterStandardTypedData).data
|
||||||
|
|
||||||
let decryptedPayload = symDecrypt(payload: encryptedPayload, aesKey: aesKey)!
|
let decryptedPayload = symDecrypt(payload: encryptedPayload, aesKey: aesKey)!
|
||||||
|
|
||||||
result(decryptedPayload)
|
result(decryptedPayload)
|
||||||
@ -135,11 +138,16 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
|
|||||||
return hashed.data
|
return hashed.data
|
||||||
}
|
}
|
||||||
|
|
||||||
func symKeygen() -> Data {
|
func symKeygen(keySize : NSNumber) -> Data? {
|
||||||
let key = SymmetricKey(size: .bits256)
|
var bytes = [Int8](repeating: 0, count: keySize.intValue / 8)
|
||||||
let keyBytes = key.withUnsafeBytes {return Data(Array($0))}
|
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
|
||||||
|
|
||||||
|
if status == errSecSuccess { // Always test the status.
|
||||||
|
let keyBytes = bytes.withUnsafeBytes {return Data(Array($0))}
|
||||||
return keyBytes
|
return keyBytes
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func symEncrypt(payload : Data, aesKey : Data) -> [Data] {
|
func symEncrypt(payload : Data, aesKey : Data) -> [Data] {
|
||||||
let mac = digest(input: aesKey + payload)
|
let mac = digest(input: aesKey + payload)
|
||||||
@ -176,5 +184,4 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user