Skip to content

Instantly share code, notes, and snippets.

@algrid
algrid / TableHeaderUpdateBug.swift
Created June 5, 2021 19:12
UITableView begin/endUpdates messing with tableHeaderView
import UIKit
class ViewController: UIViewController {
let fullWidth = UIScreen.main.bounds.width
let tableView = UITableView()
let headerView: UIView = {
let v = Header()
v.backgroundColor = .red
@algrid
algrid / KeychainSE.swift
Created June 5, 2019 20:34
Digital signature verification
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",
@algrid
algrid / KeychainSE.swift
Created June 5, 2019 20:25
Digital signature example calls
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))
@algrid
algrid / KeychainSE.swift
Created June 5, 2019 20:22
Calculating digital signature using a key from iOS Secure Enclave
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
@algrid
algrid / KeychainSE.swift
Created May 28, 2019 20:42
Decrypting data using eciesEncryptionCofactorVariableIVX963SHA256AESGCM
// 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
@algrid
algrid / KeychainSE.swift
Created May 28, 2019 20:35
Data encryption using eciesEncryptionCofactorVariableIVX963SHA256AESGCM
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)!
@algrid
algrid / KeychainSE.swift
Created May 14, 2019 13:42
Getting public key for a key stored in Secure Enclave and checking encryption algorithm availability
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
@algrid
algrid / KeychainSE.swift
Created May 1, 2019 15:27
Loading an EC encryption key from keychain
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?
@algrid
algrid / KeychainSE.swift
Last active June 28, 2025 11:59
Creating a cryptographic key stored in Secure Enclave
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
@algrid
algrid / KeychainBio.swift
Created April 13, 2019 15:45
Reading a biometry-protected entry in iOS keychain - using LAContext
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") {