From b547126708d98acca52e14c68968b8ceb8f4c77a Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sun, 5 Oct 2025 23:54:21 +0200 Subject: [PATCH] Added support for the Crypto and CommonCrypto libraries for the GenerateHashUseCase use case in the library target, so this code builds for non-apple platforms. --- Package.swift | 8 ++--- .../Use Cases/GenerateHashUseCase.swift | 31 ++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index dfa272cc..a80b3bef 100644 --- a/Package.swift +++ b/Package.swift @@ -30,21 +30,21 @@ let package = Package( ) ], dependencies: [ + .package(url: "https://github.com/apple/swift-crypto.git", from: "3.13.0"), .package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.3.0"), .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.5.0"), .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.2"), - .package(url: "https://github.com/apple/swift-crypto.git", from: "3.13.0"), .package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0") ], targets: [ .target( name: MarvelService.target, dependencies: [ - .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), .product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [ .android, .linux, .openbsd, .windows - ])) + ])), + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession") ], path: "Sources/MarvelService", plugins: [ diff --git a/Sources/MarvelService/Internal/Use Cases/GenerateHashUseCase.swift b/Sources/MarvelService/Internal/Use Cases/GenerateHashUseCase.swift index 71b3974a..c40b2f37 100644 --- a/Sources/MarvelService/Internal/Use Cases/GenerateHashUseCase.swift +++ b/Sources/MarvelService/Internal/Use Cases/GenerateHashUseCase.swift @@ -10,7 +10,13 @@ // //===----------------------------------------------------------------------=== +#if canImport(CriptoKit) import CryptoKit +#elseif canImport(Crypto) +import Crypto +#else +import CommonCrypto +#endif import struct Foundation.Data import struct Foundation.TimeInterval @@ -51,10 +57,33 @@ struct GenerateHashUseCase { let stringToHash = timestamp.asString + self.privateKey + self.publicKey let dataToHash = Data(stringToHash.utf8) +#if canImport(CriptoKit) || canImport(Crypto) return Insecure.MD5 .hash(data: dataToHash) - .map { String(format: "%02x", $0) } + .map { String(format: .Format.hexadecimal, $0) } .joined() +#else + return dataToHash + .withUnsafeBytes { + var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) + + CC_MD5($0.baseAddress, CC_LONG(dataToHash.count), &hash) + + return hash + } + .map { String(format: .Format.hexadecimal, $0) } + .joined() +#endif } } + +// MARK: - Constants + +private extension String { + /// A namespace assigned to string format representations. + enum Format { + /// A string format for MD5 hash hexadecimal bytes. + static let hexadecimal = "%02x" + } +}