Skip to content

Instantly share code, notes, and snippets.

@cardosoedgar
Last active October 17, 2022 23:43
Show Gist options
  • Save cardosoedgar/8528d50c49dbcb595e47d2df0c9674c5 to your computer and use it in GitHub Desktop.
Save cardosoedgar/8528d50c49dbcb595e47d2df0c9674c5 to your computer and use it in GitHub Desktop.
func generateSecureEnclaveKey() -> SecKey? {
let attributes: [String: Any] = [
// key id
kSecAttrApplicationTag as String: "com.cardosoedgar.key",
// key size
kSecAttrKeySizeInBits as String: 256,
// key type
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
// attribute to specify this key should be stored on Secure Enclave
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave
]
var error: Unmanaged<CFError>?
guard let ref = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
print(error.debugDescription)
return nil
}
return ref
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment