<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CryptoKit",
  "identifier" : "/documentation/CryptoKit/SymmetricKey/init(size:initializingWith:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Apple CryptoKit"
    ],
    "preciseIdentifier" : "s:9CryptoKit12SymmetricKeyV4size16initializingWithAcA0cD4SizeV_ys13OutputRawSpanVzxYKXEtxYKcs5ErrorRzlufc"
  },
  "title" : "init(size:initializingWith:)"
}
-->

# init(size:initializingWith:)

Creates a new key of the given size where the key contents are initialized via a callback.

```
init<E>(size: SymmetricKeySize, initializingWith callback: @_lifetime(0: copy 0) (inout OutputRawSpan) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

`size`

The size of the key to generate. You can use one of the standard
sizes, like [`bits256`](/documentation/CryptoKit/SymmetricKeySize/bits256), or you can create a key of
custom length by initializing a [`SymmetricKeySize`](/documentation/CryptoKit/SymmetricKeySize) instance with a
non-standard value.

`callback`

A callback that will be invoked to initialize the contents
of the key. It must initialize the full set of size.bitCount / 8 bytes
in the provided output span.

---

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)