Skip to content

Instantly share code, notes, and snippets.

@cardosoedgar
Last active October 17, 2022 23:52
Show Gist options
  • Save cardosoedgar/7b83257c2feaab0b915c1a6a9a00a2f2 to your computer and use it in GitHub Desktop.
Save cardosoedgar/7b83257c2feaab0b915c1a6a9a00a2f2 to your computer and use it in GitHub Desktop.
// if you already have the private key
let publicKey = SecKeyCopyPublicKey(privateKey)
// if you don't have the private key
func getPublicKey() -> SecKey? {
let query: [String: Any] = [
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: "com.cardosoedgar.key",
kSecReturnRef as String: true]
var keyRef: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &keyRef)
guard status == errSecSuccess else {
return nil
}
let privateKey = keyRef as! SecKey
return SecKeyCopyPublicKey(privateKey)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment