SwiftySimpleKeychain

public extension SwiftySimpleKeychain

An extension of SwiftySimpleKeychain to deal with generation, retrieving and deletion of public / private RSA keys using the Keychain. It honours the configuration (and access control) of SwiftySimpleKeychain

  • Generates a RSA key pair with the specified key size and stores it in the Keychain

    Usage:

      keychain = SwiftySimpleKeychain(with: "service-name")
      let result = keychain.generateRSAKeyPair()
      if case let .failure(error) = result {
          print(error)
      }
    

    Declaration

    Swift

    func generateRSAKeyPair(with length: SwiftySimpleKeychainRSAKeySize = .size2048Bits,
                            publicKeyTag: String = "public",
                            privateKeyTag : String = "private"
    ) -> VoidErrorResult

    Parameters

    length

    the length (int bits) of the RSA keys. Defaults to 2048 bits

    publicKeyTag

    a tag (identifier) for the public key. Defaults to public

    privateKeyTag

    a tag (identifier) for the private key. Defaults to private

  • Finds a Key stored in the Keychain and returns a DataErrorResult Use this method if you would like to know why fetching a key fails, and an error is present in the failure response.

    Usage:

     let result = keychain.rsaKeyDataResult(with: "publicKeyTag")
     guard case let .success(publicKeyData) = publicKeyDataResult else {
         if case let .failure(error) = publicKeyDataResult {
             print(error)
             fail("Fail to get Public Key. \(error)")
         }
         return
     }
    

    Declaration

    Swift

    func rsaKeyDataResult(with tag: String) -> DataErrorResult

    Parameters

    tag

    a tag (identifier) for the key.

  • Finds a Key stored in the Keychain and returns a Data if found Use this method if you want a simple response (data or zero) regardless of error on failure

    Usage:

     let data = keychain.rsaKeyData(with: "publicKeyTag")
    

    Declaration

    Swift

    func rsaKeyData(with tag: String) -> Data?

    Parameters

    tag

    a tag (identifier) for the key.

  • Cheks if there is a key with the provided tag

    Declaration

    Swift

    func hasRSAKey(with tag: String) -> Bool

    Parameters

    tag

    a tag (identifier) for the key.

  • Deletes a RSA Key identified with the provided tag

    Usage:

     let deleted = keychain.deleteRSAKey(with: "publicKeyTag")
    

    Declaration

    Swift

    func deleteRSAKey(with tag: String) -> Bool

    Parameters

    tag

    a tag (identifier) for the key.

  • Finds a Key stored in the Keychain and returns a SecKeyErrorResult

      let result = keychain.rsaSecKeyResult(with: "publicTag")
      if case let .failure(error) = result {
          print(error)
      }
    

    Declaration

    Swift

    func rsaSecKeyResult(with tag: String) -> SecKeyErrorResult

    Parameters

    tag

    a tag (identifier) for the key.

  • Finds a Key stored in the Keychain and returns a SecKeyErrorResult

      if let publicKey = keychain.rsaSecKey(with: "publicTag") {
    
      }
    

    Declaration

    Swift

    func rsaSecKey(with tag: String) -> SecKey?

    Parameters

    tag

    a tag (identifier) for the key.