Skip to content

Commit eb4e72e

Browse files
committed
ContainerRegistry: Move digest() to its own file
1 parent b8c0178 commit eb4e72e

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

Sources/ContainerRegistry/Blobs.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@
1414

1515
import Foundation
1616
import HTTPTypes
17-
import struct Crypto.SHA256
18-
19-
/// Calculates the digest of a blob of data.
20-
/// - Parameter data: Blob of data to digest.
21-
/// - Returns: The blob's digest, in the format expected by the distribution protocol.
22-
public func digest<D: DataProtocol>(of data: D) -> ImageReference.Digest {
23-
// SHA256 is required; some registries might also support SHA512
24-
let hash = SHA256.hash(data: data)
25-
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
26-
return try! ImageReference.Digest("sha256:" + digest)
27-
}
2817

2918
extension RegistryClient {
3019
// Internal helper method to initiate a blob upload in 'two shot' mode
@@ -61,9 +50,6 @@ extension RegistryClient {
6150
}
6251
}
6352

64-
// The spec says that Docker- prefix headers are no longer to be used, but also specifies that the registry digest is returned in this header.
65-
extension HTTPField.Name { static let dockerContentDigest = Self("Docker-Content-Digest")! }
66-
6753
public extension RegistryClient {
6854
func blobExists(repository: ImageReference.Repository, digest: ImageReference.Digest) async throws -> Bool {
6955
do {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftContainerPlugin open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
import HTTPTypes
17+
import struct Crypto.SHA256
18+
19+
// The spec says that Docker- prefix headers are no longer to be used, but also specifies that the registry digest is returned in this header.
20+
extension HTTPField.Name { static let dockerContentDigest = Self("Docker-Content-Digest")! }
21+
22+
/// Calculates the digest of a blob of data.
23+
/// - Parameter data: Blob of data to digest.
24+
/// - Returns: The blob's digest, in the format expected by the distribution protocol.
25+
public func digest(of data: any DataProtocol) -> ImageReference.Digest {
26+
// SHA256 is required; some registries might also support SHA512
27+
let hash = SHA256.hash(data: data)
28+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
29+
return try! ImageReference.Digest("sha256:" + digest)
30+
}

0 commit comments

Comments
 (0)