Add pbkdf2 sha1 and keypair gen on iOS

This commit is contained in:
Hugo Pointcheval 2020-04-29 22:27:21 +02:00
parent 0656a06eab
commit 5e30e36550
2 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -7,6 +7,7 @@
import Flutter import Flutter
import UIKit import UIKit
import CommonCrypto import CommonCrypto
import Security
extension FlutterStandardTypedData { extension FlutterStandardTypedData {
var uint8Array: Array<UInt8> { var uint8Array: Array<UInt8> {
@ -19,6 +20,17 @@ extension FlutterStandardTypedData {
} }
} }
@available(iOS 10.0, *)
func generateKeypair() {
var publicKeySec, privateKeySec: SecKey?
let keyattribute = [
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeySizeInBits as String : 256
] as CFDictionary
SecKeyGeneratePair(keyattribute, &publicKeySec, &privateKeySec)
}
func crypt(operation: Int, algorithm: Int, options: Int, key: Data, func crypt(operation: Int, algorithm: Int, options: Int, key: Data,
initializationVector: Data, dataIn: Data) -> Data? { initializationVector: Data, dataIn: Data) -> Data? {
return key.withUnsafeBytes { keyUnsafeRawBufferPointer in return key.withUnsafeBytes { keyUnsafeRawBufferPointer in
@ -74,6 +86,11 @@ func pbkdf2sha256(password: String, salt: String, keyByteCount: Int, rounds: Int
return pbkdf2(hash: CCPBKDFAlgorithm(kCCPRFHmacAlgSHA256), password: password, salt: salt, keyByteCount: keyByteCount, rounds: rounds) return pbkdf2(hash: CCPBKDFAlgorithm(kCCPRFHmacAlgSHA256), password: password, salt: salt, keyByteCount: keyByteCount, rounds: rounds)
} }
func pbkdf2sha1(password: String, salt: String, keyByteCount: Int, rounds: Int) -> Data? {
return pbkdf2(hash: CCPBKDFAlgorithm(kCCPRFHmacAlgSHA1), password: password, salt: salt, keyByteCount: keyByteCount, rounds: rounds)
}
func randomGenerateBytes(count: Int) -> Data? { func randomGenerateBytes(count: Int) -> Data? {
let bytes = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) let bytes = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
defer { bytes.deallocate() } defer { bytes.deallocate() }
@ -151,8 +168,15 @@ public class SwiftNativeCryptoPlugin: NSObject, FlutterPlugin {
let salt = args["salt"] as! String let salt = args["salt"] as! String
let keyLength = args["keyLength"] as! NSNumber let keyLength = args["keyLength"] as! NSNumber
let iteration = args["iteration"] as! NSNumber let iteration = args["iteration"] as! NSNumber
let algo = args["algorithm"] as! String
let keyBytes = pbkdf2sha256(password: password, salt: salt, keyByteCount: keyLength.intValue, rounds: iteration.intValue) var keyBytes: Data?
if (algo == "sha1") {
keyBytes = pbkdf2sha1(password: password, salt: salt, keyByteCount: keyLength.intValue, rounds: iteration.intValue)
} else {
keyBytes = pbkdf2sha256(password: password, salt: salt, keyByteCount: keyLength.intValue, rounds: iteration.intValue)
}
if keyBytes != nil { if keyBytes != nil {
result(FlutterStandardTypedData.init(bytes: keyBytes!)) result(FlutterStandardTypedData.init(bytes: keyBytes!))