Compare two secKey keys (public keys) in ios Swift

I want ssl to pin the public key to swift, I read a lot of examples of how to do this, and the last thing I can not find is how to compare the two public keys in the SecKey object format. Example:

let serverPublicKey = SecTrustCopyPublicKey(secTrust) /*return SecKey object from actual SecTrust*/
let clientPublicKey = getLocalPublicKeyFromDer() /*return SecKey from .der local*/

how to compare them? I am doing this now and it works:

if(serverPublicKey! as AnyObject).isEqual(clientPublicKey){
  /*Key is the same, pinning OK!*/
}

find a way on gitHub: https://github.com/teamcarma/IOS-AlamofireDomain/blob/master/Source/ServerTrustPolicy.swift

but casting to AnyObject is a good idea? How does isEqual work on cast SecKey? Can anyone explain me?

ps. Another idea is to get base64 from SecKey - I try, and it works too, but it requires temporary KeyChain operations, and they don't look professional.

+5
4

:

" SecKeychainItem SecKeyRef." *

SecKeyRef SecKeychainItem. (.. keychain),

SecKeychainItemCreatePersistentReference

CFData, . memcpy NSData isEqualToData. CFData.

Edit

iOS, , ( ) , , . , , 30 . .

I : .

0

, NSData: SecKeyRef Swift?

NSData isEqualToData:

0

iOS10 :

CFDataRef _Nullable SecKeyCopyExternalRepresentation(SecKeyRef key, CFErrorRef *error)

Now you can create two Data objects (NSData) and then compare them.

0
source

I have no experience in the domain, but if they are two lines (regardless of their content), you basically do a simple check:

if(string1 == string2)
{
 //condition logic
}
-3
source

Source: https://habr.com/ru/post/1625954/


All Articles