Adopt FoundationEssentials instead of Foundation where available (#317)

Use `FoundationEssentials` if available.

<!-- Thanks for contributing to Swift Crypto! Before you submit your
issue, please make sure you followed our checklist and check the
appropriate boxes by putting an x in the [ ]: [x] -->

### Checklist
- [x] I've run tests to see all new and existing tests pass (macOS and
linux)
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [x] I've updated the documentation if necessary

#### If you've made changes to `gyb` files
- [ ] I've run `.script/generate_boilerplate_files_with_gyb` and
included updated generated files in a commit of this pull request
```
bash .script/generate_boilerplate_files_with_gyb
bash: .script/generate_boilerplate_files_with_gyb: No such file or directory
```

### Motivation:

Would be great that packages that depend on `swift-crypto` can be built
without linking full `Foundation`.

### Modifications:

This patch simply replaces `import Foundation` with a conditional
`import FoundationEssentials`.

In some files this also required to explicitly import the C standard
lib.

Maybe we could follow the same approach as in
https://github.com/swiftlang/swift-tools-support-core/blob/main/Sources/TSCLibc/libc.swift
and add an internal module that abstracts this import dance in a single
place.

### Result:

After this change the package should only depend on
`FoundationEssentials`.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
authored and GitHub committed 2026-04-17 15:06:53 +00:00
1 parent c5b1d7f0cb
commit 2d37b1192f
6 files changed
+35 -5

No files matched your search

@@ -14,8 +14,13 @@
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
#if canImport(FoundationEssentials)
import protocol FoundationEssentials.ContiguousBytes
import struct FoundationEssentials.Data
#else
import protocol Foundation.ContiguousBytes
import struct Foundation.Data
#endif
/// A wrapper around BoringSSL's EC_POINT with some lifetime management and value semantics.
@usableFromInline
+6 -1
View File
@@ -13,9 +13,14 @@
//===----------------------------------------------------------------------===//
import Crypto
import Foundation
import SwiftASN1
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
extension Curve25519.Signing.PrivateKey {
/// A Distinguished Encoding Rules (DER) encoded representation of the private key.
@@ -13,9 +13,14 @@
//===----------------------------------------------------------------------===//
import Crypto
import Foundation
import SwiftASN1
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
extension Curve25519.Signing.PrivateKey {
/// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format.
+6 -1
View File
@@ -13,9 +13,14 @@
//===----------------------------------------------------------------------===//
import Crypto
import CryptoExtras
import Foundation
import XCTest
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
final class CMACTests: XCTestCase {
// Borrowed from CryptoKit
func testVector1() throws {
@@ -12,9 +12,14 @@
//
//===----------------------------------------------------------------------===//
import Crypto
import Foundation
import XCTest
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
#if !canImport(Darwin) || canImport(CryptoKit, _version: 241.100.42)
// Corresponds to the CryptoKit in XCode 16.3, which has Sendable annotations
import Crypto
@@ -13,11 +13,16 @@
//===----------------------------------------------------------------------===//
import Crypto
import Foundation
import XCTest
@testable import CryptoExtras
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
final class PKCS8DERRepresentationTests: XCTestCase {
func test_RSA() {
let rsaPrivateKeyPEM = """