<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "LocalAuthentication",
  "identifier" : "/documentation/LocalAuthentication/LAPrivateKey/exchangeKeys(publicKey:algorithm:parameters:completion:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Local Authentication"
    ],
    "preciseIdentifier" : "c:objc(cs)LAPrivateKey(im)exchangeKeysWithPublicKey:secKeyAlgorithm:secKeyParameters:completion:"
  },
  "title" : "exchangeKeys(publicKey:algorithm:parameters:completion:)"
}
-->

# exchangeKeys(publicKey:algorithm:parameters:completion:)

Performs a Diffie-Hellman style key exchange operation.

```
func exchangeKeys(publicKey: Data, algorithm: SecKeyAlgorithm, parameters: [AnyHashable : Any], completion handler: @escaping @Sendable (Data?, (any Error)?) -> Void)
```

## Parameters

`publicKey`

The remote party’s public key.

`algorithm`

An algorithm suitable for performing this key exchange. For example, `ecdhKeyExchangeCofactorX963SHA256`.

`parameters`

A dictionary with parameters for this key exchange.

`handler`

A completion handler to call when the key exchange operation completes.

- data: The result of the key exchange operation.
- error: An error object that indicates why the key exchange failed, or `nil` if the exchange succeeded.

## Discussion

> Important:
> You can call this method from synchronous code using a completion handler, as shown on this page, or you can call it as an asynchronous method that has the following declaration:
> 
> ```swift
> func exchangeKeys(publicKey: Data, algorithm: SecKeyAlgorithm, parameters: [AnyHashable : Any]) async throws -> Data
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

The algorithm you use determines the parameters in the dictionary that are required or optional. For more information, see <doc://com.apple.documentation/documentation/Security/SecKeyKeyExchangeParameter>.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)