mirror of
https://github.com/apple/swift-crypto
synced 2026-11-25 04:47:47 +00:00
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>
72 lines
2.5 KiB
Swift
72 lines
2.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Crypto
|
|
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.
|
|
public var pkcs8DERRepresentation: Data {
|
|
self.derRepresentation
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
|
|
extension Curve25519.KeyAgreement.PrivateKey {
|
|
/// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format.
|
|
public var pkcs8DERRepresentation: Data {
|
|
self.derRepresentation
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
|
|
extension P256.Signing.PrivateKey {
|
|
/// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format.
|
|
///
|
|
/// This property provides the same output as the existing `derRepresentation` property,
|
|
/// which already conforms to the PKCS#8 standard.
|
|
public var pkcs8DERRepresentation: Data {
|
|
self.derRepresentation
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
|
|
extension P384.Signing.PrivateKey {
|
|
/// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format.
|
|
///
|
|
/// This property provides the same output as the existing `derRepresentation` property,
|
|
/// which already conforms to the PKCS#8 standard.
|
|
public var pkcs8DERRepresentation: Data {
|
|
self.derRepresentation
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
|
|
extension P521.Signing.PrivateKey {
|
|
/// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format.
|
|
///
|
|
/// This property provides the same output as the existing `derRepresentation` property,
|
|
/// which already conforms to the PKCS#8 standard.
|
|
public var pkcs8DERRepresentation: Data {
|
|
self.derRepresentation
|
|
}
|
|
}
|