From the course: iOS Development: Security

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Retrieving the private key from the Keychain

Retrieving the private key from the Keychain - iOS Tutorial

From the course: iOS Development: Security

Start my 1-month free trial

Retrieving the private key from the Keychain

- [Instructor] We should prevent generating multiple keys for the same tag. Thus I'm going to add a method that checks whether a key with a given tag has been added to the keychain. It's a private method. Private func, and I'm going to call it retrievePrivateKey. The method returns an optional cryptographic key object and it may throw an error. Next, I create a query dictionary by providing the attributes that identify the key. Let privateKeyQuery. it has a key of type string and the value should be any, and now let's initialize it. The items class should be kSecClass key, which denotes a cryptographic key item. Again, I need to cast it to a string in order to use it in a Swift dictionary, and the value is kSecClassKey. The application tag is the unique tag data. So, for the key kSecAttrApplicationTag, again as string. I'm going to set the tag data. The key type should be of type RSA. kSecAttrKeyType and I set it to a constant which translates to RSA keys. And finally I indicate that…

Contents