diff --git a/BUILD b/BUILD index f6e1b93ccf..eaf5223c5e 100644 --- a/BUILD +++ b/BUILD @@ -95,7 +95,7 @@ swift_library( "@sourcekitten_com_github_jpsim_yams//:Yams", "@swiftlint_com_github_scottrhoyt_swifty_text_table//:SwiftyTextTable", ] + select({ - "@platforms//os:linux": ["@com_github_krzyzanowskim_cryptoswift//:CryptoSwift"], + "@platforms//os:linux": ["@com_github_apple_swift_crypto//:Crypto"], "//conditions:default": [":DyldWarningWorkaround"], }), ) diff --git a/MODULE.bazel b/MODULE.bazel index 8ec7b30033..78bac69eb8 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -10,6 +10,7 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1") bazel_dep(name = "platforms", version = "0.0.10") bazel_dep(name = "rules_apple", version = "3.8.0", repo_name = "build_bazel_rules_apple") bazel_dep(name = "rules_swift", version = "2.1.1", repo_name = "build_bazel_rules_swift") +bazel_dep(name = "rules_cc", version = "0.0.16", repo_name = "build_bazel_rules_cc") bazel_dep(name = "sourcekitten", version = "0.36.0", repo_name = "com_github_jpsim_sourcekitten") bazel_dep(name = "swift-syntax", version = "600.0.0", repo_name = "SwiftSyntax") bazel_dep(name = "swift_argument_parser", version = "1.3.1.1", repo_name = "sourcekitten_com_github_apple_swift_argument_parser") @@ -19,7 +20,7 @@ swiftlint_repos = use_extension("//bazel:repos.bzl", "swiftlint_repos_bzlmod") use_repo( swiftlint_repos, "com_github_johnsundell_collectionconcurrencykit", - "com_github_krzyzanowskim_cryptoswift", + "com_github_apple_swift_crypto", "swiftlint_com_github_scottrhoyt_swifty_text_table", ) diff --git a/Package.resolved b/Package.resolved index ee63d9748b..1e3ccf000a 100644 --- a/Package.resolved +++ b/Package.resolved @@ -9,15 +9,6 @@ "version" : "0.2.0" } }, - { - "identity" : "cryptoswift", - "kind" : "remoteSourceControl", - "location" : "https://github.com/krzyzanowskim/CryptoSwift.git", - "state" : { - "revision" : "c9c3df6ab812de32bae61fc0cd1bf6d45170ebf0", - "version" : "1.8.2" - } - }, { "identity" : "sourcekitten", "kind" : "remoteSourceControl", @@ -36,6 +27,24 @@ "version" : "1.3.1" } }, + { + "identity" : "swift-asn1", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-asn1.git", + "state" : { + "revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6", + "version" : "1.3.0" + } + }, + { + "identity" : "swift-crypto", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-crypto.git", + "state" : { + "revision" : "06dc63c6d8da54ee11ceb268cde1fa68161afc96", + "version" : "3.9.1" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index 2789c2fef6..badd131867 100644 --- a/Package.swift +++ b/Package.swift @@ -35,7 +35,7 @@ let package = Package( .package(url: "https://github.com/jpsim/Yams.git", from: "5.0.6"), .package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.9.0"), .package(url: "https://github.com/JohnSundell/CollectionConcurrencyKit.git", from: "0.2.0"), - .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.8.0")), + .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMinor(from: "3.9.1")), ], targets: [ .plugin( @@ -68,7 +68,7 @@ let package = Package( .target( name: "SwiftLintCore", dependencies: [ - .product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: [.linux])), + .product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux])), .target(name: "DyldWarningWorkaround", condition: .when(platforms: [.macOS])), .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "SwiftIDEUtils", package: "swift-syntax"), diff --git a/Source/SwiftLintCore/Extensions/Configuration+Cache.swift b/Source/SwiftLintCore/Extensions/Configuration+Cache.swift index c3e575a8b4..271a9d8d5f 100644 --- a/Source/SwiftLintCore/Extensions/Configuration+Cache.swift +++ b/Source/SwiftLintCore/Extensions/Configuration+Cache.swift @@ -1,6 +1,3 @@ -#if canImport(CryptoSwift) -import CryptoSwift -#endif import Foundation extension Configuration { diff --git a/Source/SwiftLintCore/Extensions/String+sha256.swift b/Source/SwiftLintCore/Extensions/String+sha256.swift index c4787faedc..53797e7fa5 100644 --- a/Source/SwiftLintCore/Extensions/String+sha256.swift +++ b/Source/SwiftLintCore/Extensions/String+sha256.swift @@ -1,14 +1,14 @@ -#if canImport(CommonCrypto) -import CommonCrypto +#if canImport(CryptoKit) +import CryptoKit +#elseif canImport(Crypto) +import Crypto +#endif + import Foundation extension Data { internal func sha256() -> Data { - withUnsafeBytes { bytes in - var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) - _ = CC_SHA256(bytes.baseAddress, CC_LONG(count), &hash) - return Data(hash) - } + Data(SHA256.hash(data: self)) } internal func toHexString() -> String { @@ -18,7 +18,6 @@ extension Data { extension String { internal func sha256() -> String { - data(using: .utf8)!.sha256().toHexString() + Data(utf8).sha256().toHexString() } } -#endif diff --git a/Source/SwiftLintCore/Reporters/CodeClimateReporter.swift b/Source/SwiftLintCore/Reporters/CodeClimateReporter.swift index 90a622d2c1..6800ac6d54 100644 --- a/Source/SwiftLintCore/Reporters/CodeClimateReporter.swift +++ b/Source/SwiftLintCore/Reporters/CodeClimateReporter.swift @@ -1,6 +1,3 @@ -#if canImport(CryptoSwift) -import CryptoSwift -#endif import Foundation import SourceKittenFramework diff --git a/bazel/CryptoSwift.BUILD b/bazel/CryptoSwift.BUILD deleted file mode 100644 index d49768e962..0000000000 --- a/bazel/CryptoSwift.BUILD +++ /dev/null @@ -1,11 +0,0 @@ -load( - "@build_bazel_rules_swift//swift:swift.bzl", - "swift_library", -) - -swift_library( - name = "CryptoSwift", - srcs = glob(["Sources/**/*.swift"]), - module_name = "CryptoSwift", - visibility = ["//visibility:public"], -) diff --git a/bazel/SwiftCrypto.BUILD b/bazel/SwiftCrypto.BUILD new file mode 100644 index 0000000000..bad6df823f --- /dev/null +++ b/bazel/SwiftCrypto.BUILD @@ -0,0 +1,86 @@ +""" +Swift Crypto is an open-source implementation of a substantial portion of the API of +[Apple CryptoKit](https://developer.apple.com/documentation/cryptokit) suitable for +use on Linux platforms. It enables cross-platform or server applications with the +advantages of CryptoKit. + +This build overlay is borrowed from aaronsky/asc-swift: +https://github.com/aaronsky/asc-swift/blob/386bb669/bazel/third_party/com_github_apple_swift_crypto/BUILD.overlay +""" + +load("@build_bazel_rules_cc//cc:defs.bzl", "cc_library") +load("@build_bazel_rules_swift//swift:swift_interop_hint.bzl", "swift_interop_hint") +load("@build_bazel_rules_swift//swift:swift_library.bzl", "swift_library") + +_crypto_non_apple_deps = [ + ":CCryptoBoringSSL", + ":CCryptoBoringSSLShims", + ":CryptoBoringWrapper", +] + +swift_library( + name = "Crypto", + srcs = glob(["Sources/Crypto/**/*.swift"]), + defines = [ + "MODULE_IS_CRYPTO", + "CRYPTO_IN_SWIFTPM", + ] + select({ + "@platforms//os:linux": ["CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"], + "@platforms//os:android": ["CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"], + "@platforms//os:windows": ["CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"], + "@platforms//os:wasi": ["CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"], + "//conditions:default": [], + }), + module_name = "Crypto", + visibility = ["//visibility:public"], + deps = select({ + "@platforms//os:linux": _crypto_non_apple_deps, + "@platforms//os:android": _crypto_non_apple_deps, + "@platforms//os:windows": _crypto_non_apple_deps, + "@platforms//os:wasi": _crypto_non_apple_deps, + "//conditions:default": [], + }), +) + +cc_library( + name = "CCryptoBoringSSL", + srcs = glob([ + "Sources/CCryptoBoringSSL/crypto/**/*.c", + "Sources/CCryptoBoringSSL/crypto/**/*.h", + "Sources/CCryptoBoringSSL/crypto/**/*.S", + "Sources/CCryptoBoringSSL/third_party/**/*.h", + "Sources/CCryptoBoringSSL/third_party/**/*.S", + ]), + hdrs = glob(["Sources/CCryptoBoringSSL/include/**/*.h"]), + aspect_hints = [":CCryptoBoringSSL_interop"], + includes = ["Sources/CCryptoBoringSSL/include"], +) + +swift_interop_hint( + name = "CCryptoBoringSSL_interop", + module_name = "CCryptoBoringSSL", +) + +cc_library( + name = "CCryptoBoringSSLShims", + srcs = ["Sources/CCryptoBoringSSLShims/shims.c"], + hdrs = ["Sources/CCryptoBoringSSLShims/include/CCryptoBoringSSLShims.h"], + aspect_hints = [":CCryptoBoringSSLShims_interop"], + includes = ["Sources/CCryptoBoringSSLShims/include"], + deps = [":CCryptoBoringSSL"], +) + +swift_interop_hint( + name = "CCryptoBoringSSLShims_interop", + module_name = "CCryptoBoringSSLShims", +) + +swift_library( + name = "CryptoBoringWrapper", + srcs = glob(["Sources/CryptoBoringWrapper/**/*.swift"]), + module_name = "CryptoBoringWrapper", + deps = [ + ":CCryptoBoringSSL", + ":CCryptoBoringSSLShims", + ], +) diff --git a/bazel/repos.bzl b/bazel/repos.bzl index 447e216d8b..f7fc1802ed 100644 --- a/bazel/repos.bzl +++ b/bazel/repos.bzl @@ -57,11 +57,11 @@ def swiftlint_repos(bzlmod = False): ) http_archive( - name = "com_github_krzyzanowskim_cryptoswift", - sha256 = "3d649cccfe9ae0572163cde0201f013d10349a035c15225e7a4bd83c85fb0d1d", - build_file = "@SwiftLint//bazel:CryptoSwift.BUILD", - strip_prefix = "CryptoSwift-1.8.0", - url = "https://github.com/krzyzanowskim/CryptoSwift/archive/refs/tags/1.8.0.tar.gz", + name = "com_github_apple_swift_crypto", + build_file = "@SwiftLint//bazel:SwiftCrypto.BUILD", + sha256 = "f4a012b5b1898574bd4426855af65a9d1934c9d13a73e3a0586e129069a376f3", + strip_prefix = "swift-crypto-3.9.1", + url = "https://github.com/apple/swift-crypto/archive/refs/tags/3.9.1.zip", ) def _swiftlint_repos_bzlmod(_):