Last active
December 19, 2020 01:34
-
-
Save dfpalomar/0322a839ba2bd4b5959be3ca9d58c3eb to your computer and use it in GitHub Desktop.
Wrapper around swift framework to make it compatible with Kotlin MPP projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@objc public class CryptoBridge: NSObject { | |
@objc public func generateKeyPair() -> BKeyPair { | |
let keyPair = RSA.generateKeyPair(keySize: RSA.KeySize.RSA2048) | |
return BKeyPair( | |
privateKey: (keyPair?.privateKey.dataRepresentation())!, | |
publicKey: (keyPair?.publicKey.dataRepresentation())! | |
) | |
} | |
@objc public func generateAESKey(keySize: Int) -> Data { | |
return Data.secureRandomBytes(keySize)! | |
} | |
@objc public func symmetricEncrypt(aesKey: Data, plainData: Data) -> BSymmetricEncryptedData { | |
let ivData = generateRandomIV(count: 16) | |
let encryptedData = AES.encrypt(data: plainData, key: aesKey, iv: ivData, keySize: .AES256) | |
return BSymmetricEncryptedData(data: encryptedData!, iv: ivData) | |
} | |
@objc public func symmetricDecrypt(aesKey: Data, encryptedData: BSymmetricEncryptedData) -> Data { | |
return AES.decrypt(data: encryptedData.data, key: aesKey, iv: encryptedData.iv, keySize: .AES256)! | |
} | |
@objc public func asymmetricEncrypt(publicKey: Data, plainData: Data) -> Data { | |
let pairAttributes = [ | |
String(kSecAttrKeyType): kSecAttrKeyTypeRSA, | |
String(kSecAttrKeySizeInBits): RSA.KeySize.RSA2048.rawValue | |
] as [String: Any] | |
let secKey = SecKeyCreateWithData(publicKey as NSData, pairAttributes as CFDictionary, nil) | |
do { | |
return try RSA.encrypt(plainData, publicKey: secKey!)!; | |
} catch { | |
fatalError("asymmetricDecrypt FAILED") | |
} | |
} | |
@objc public func asymmetricDecrypt(privateKey: Data, encryptedData: Data) -> Data { | |
let pairAttributes = [ | |
String(kSecAttrKeyType): kSecAttrKeyTypeRSA, | |
String(kSecAttrKeySizeInBits): RSA.KeySize.RSA2048.rawValue | |
] as [String: Any] | |
let secKey = SecKeyCreateWithData(privateKey as NSData, pairAttributes as CFDictionary, nil) | |
do { | |
return try RSA.decrypt(encryptedData, privateKey: secKey!)! | |
} catch { | |
fatalError("asymmetricDecrypt FAILED") | |
} | |
} | |
@objc public func generateRandomIV(count: Int) -> Data { | |
return Data.secureRandomBytes(count)! | |
} | |
} | |
extension String: LocalizedError { | |
public var errorDescription: String? { return self } | |
} | |
@objc public class BKeyPair: NSObject { | |
let privateKey: Data | |
let publicKey: Data | |
public init(privateKey: Data, publicKey: Data) { | |
self.privateKey = privateKey | |
self.publicKey = publicKey | |
} | |
} | |
@objc public class BSymmetricEncryptedData: NSObject { | |
let data: Data | |
let iv: Data | |
public init(data: Data, iv: Data) { | |
self.data = data | |
self.iv = iv | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CryptoCocoa-Swift.h