This file contains hidden or 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
| import UIKit | |
| class ViewController: UIViewController { | |
| let fullWidth = UIScreen.main.bounds.width | |
| let tableView = UITableView() | |
| let headerView: UIView = { | |
| let v = Header() | |
| v.backgroundColor = .red |
This file contains hidden or 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
| guard let publicKey = SecKeyCopyPublicKey(key!) else { | |
| UIAlertController.showSimple(title: "Can't verify signature", | |
| text: "Can't get public key", | |
| from: self) | |
| return | |
| } | |
| let algorithm: SecKeyAlgorithm = .ecdsaSignatureMessageX962SHA256 | |
| guard SecKeyIsAlgorithmSupported(publicKey, .verify, algorithm) else { | |
| UIAlertController.showSimple(title: "Can't verify signature", |
This file contains hidden or 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
| let clearTextData = clearText.data(using: .utf8)! | |
| // Signing the original message | |
| sign(algorithm: .ecdsaSignatureMessageX962SHA256, data: clearTextData) | |
| // Signing precalculated sha256 hash of the original message (produces the same result) | |
| sign(algorithm: .ecdsaSignatureDigestX962SHA256, data: sha256(data: clearTextData)) |
This file contains hidden or 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
| private func sign(algorithm: SecKeyAlgorithm, data: Data) { | |
| guard SecKeyIsAlgorithmSupported(key!, .sign, algorithm) else { | |
| UIAlertController.showSimple(title: "Can't sign", | |
| text: "Algorith not supported", | |
| from: self) | |
| return | |
| } | |
| // SecKeyCreateSignature call is blocking when the used key |
This file contains hidden or 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
| // cipherTextData is our encrypted data | |
| let algorithm: SecKeyAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM | |
| guard SecKeyIsAlgorithmSupported(self.key!, .decrypt, algorithm) else { | |
| UIAlertController.showSimple(title: "Can't decrypt", | |
| text: "Algorith not supported", from: self) | |
| return | |
| } | |
| // SecKeyCreateDecryptedData call is blocking when the used key |
This file contains hidden or 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
| let clearText = "Hello" | |
| let algorithm: SecKeyAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM | |
| guard SecKeyIsAlgorithmSupported(publicKey, .encrypt, algorithm) else { | |
| UIAlertController.showSimple(title: "Can't encrypt", | |
| text: "Algorith not supported", from: self) | |
| return | |
| } | |
| var error: Unmanaged<CFError>? | |
| let clearTextData = clearText.data(using: .utf8)! |
This file contains hidden or 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
| guard let publicKey = SecKeyCopyPublicKey(key) else { | |
| // Can't get public key | |
| return | |
| } | |
| let algorithm: SecKeyAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM | |
| guard SecKeyIsAlgorithmSupported(publicKey, .encrypt, algorithm) else { | |
| // Algorith not supported | |
| return | |
| } | |
| // Now we're ready to encrypt data using publicKey |
This file contains hidden or 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
| static func loadKey(name: String) -> SecKey? { | |
| let tag = name.data(using: .utf8)! | |
| let query: [String: Any] = [ | |
| kSecClass as String : kSecClassKey, | |
| kSecAttrApplicationTag as String : tag, | |
| kSecAttrKeyType as String : kSecAttrKeyTypeEC, | |
| kSecReturnRef as String : true | |
| ] | |
| var item: CFTypeRef? |
This file contains hidden or 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
| static func makeAndStoreKey(name: String, | |
| requiresBiometry: Bool = false) throws -> SecKey { | |
| let flags: SecAccessControlCreateFlags | |
| if #available(iOS 11.3, *) { | |
| flags = requiresBiometry ? | |
| [.privateKeyUsage, .biometryCurrentSet] : .privateKeyUsage | |
| } else { | |
| flags = requiresBiometry ? | |
| [.privateKeyUsage, .touchIDCurrentSet] : .privateKeyUsage |
This file contains hidden or 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
| checkBiometryState { success in | |
| guard success else { | |
| // Biometric authentication is not available | |
| return | |
| } | |
| let authContext = LAContext() | |
| let accessControl = KeychainHelper.getBioSecAccessControl() | |
| authContext.evaluateAccessControl(accessControl, | |
| operation: .useItem, | |
| localizedReason: "Access sample keychain entry") { |
NewerOlder