Skip to content

Commit 7fd6e41

Browse files
committed
ContainerRegistry: Make digest(of:Data) an ImageReference.Digest constructor
1 parent 83852d0 commit 7fd6e41

File tree

6 files changed

+54
-48
lines changed

6 files changed

+54
-48
lines changed

Sources/ContainerRegistry/Blobs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public extension RegistryClient {
119119
// Append the digest to the upload location, as the specification requires.
120120
// The server's URL is arbitrary and might already contain query items which we must not overwrite.
121121
// The URL could even point to a different host.
122-
let digest = digest(of: data)
122+
let digest = ImageReference.Digest(of: data)
123123
let uploadURL = location.appending(queryItems: [.init(name: "digest", value: "\(digest)")])
124124

125125
let httpResponse = try await executeRequestThrowing(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import struct Crypto.SHA512
19+
20+
// The distribution spec says that Docker- prefix headers are no longer to be used,
21+
// but also specifies that the registry digest is returned in this header.
22+
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests
23+
extension HTTPField.Name {
24+
static let dockerContentDigest = Self("Docker-Content-Digest")!
25+
}
26+
27+
/// Calculate the digest of a blob of data.
28+
/// - Parameters:
29+
/// - data: Blob of data to digest.
30+
/// - algorithm: Digest algorithm to use.
31+
/// - Returns: The blob's digest, in the format expected by the distribution protocol.
32+
extension ImageReference.Digest {
33+
public init(
34+
of data: any DataProtocol,
35+
algorithm: ImageReference.Digest.Algorithm = .sha256
36+
) {
37+
// SHA256 is required; some registries might also support SHA512
38+
switch algorithm {
39+
case .sha256:
40+
let hash = SHA256.hash(data: data)
41+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
42+
try! self.init("sha256:" + digest)
43+
44+
case .sha512:
45+
let hash = SHA512.hash(data: data)
46+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
47+
try! self.init("sha512:" + digest)
48+
}
49+
}
50+
}

Sources/ContainerRegistry/Manifests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public extension RegistryClient {
2121
// See https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pushing-manifests
2222

2323
let encoded = try encoder.encode(manifest)
24-
let digest = digest(of: encoded)
24+
let digest = ImageReference.Digest(of: encoded)
2525
let mediaType = manifest.mediaType ?? "application/vnd.oci.image.manifest.v1+json"
2626

2727
let _ = try await executeRequestThrowing(

Sources/ContainerRegistry/RegistryClient+Digest.swift

Lines changed: 0 additions & 44 deletions
This file was deleted.

Sources/containertool/Extensions/RegistryClient+Layers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension RegistryClient {
5050
mediaType: String = "application/vnd.oci.image.layer.v1.tar+gzip"
5151
) async throws -> ImageLayer {
5252
// The diffID is the hash of the unzipped layer tarball
53-
let diffID = digest(of: contents)
53+
let diffID = ImageReference.Digest(of: contents)
5454
// The layer blob is the gzipped tarball; the descriptor is the hash of this gzipped blob
5555
let blob = Data(gzip(contents))
5656
let descriptor = try await putBlob(repository: repository, mediaType: mediaType, data: blob)

Sources/containertool/containertool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ extension RegistryClient {
254254
forImage: baseImage,
255255
architecture: architecture
256256
)
257-
//try log("Found base image manifest: \(digest(of: baseImageManifest))")
257+
//try log("Found base image manifest: \(ImageReference.Digest(of: baseImageManifest))")
258258

259259
baseImageConfiguration = try await source.getImageConfiguration(
260260
forImage: baseImage,

0 commit comments

Comments
 (0)