Skip to content

Commit 95fa156

Browse files
committed
ContainerRegistry: Add sha512 digest
1 parent 8ef576c commit 95fa156

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Sources/ContainerRegistry/RegistryClient+Digest.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@
1515
import Foundation
1616
import HTTPTypes
1717
import struct Crypto.SHA256
18+
import struct Crypto.SHA512
1819

1920
// 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.
2021
extension HTTPField.Name { static let dockerContentDigest = Self("Docker-Content-Digest")! }
2122

2223
/// Calculates the digest of a blob of data.
23-
/// - Parameter data: Blob of data to digest.
24+
/// - Parameters:
25+
/// - data: Blob of data to digest.
26+
/// - algorithm: Digest algorithm to use.
2427
/// - Returns: The blob's digest, in the format expected by the distribution protocol.
25-
public func digest(of data: any DataProtocol) -> ImageReference.Digest {
28+
public func digest(
29+
of data: any DataProtocol,
30+
algorithm: ImageReference.Digest.Algorithm = .sha256
31+
) -> ImageReference.Digest {
2632
// 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)
33+
switch algorithm {
34+
case .sha256:
35+
let hash = SHA256.hash(data: data)
36+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
37+
return try! ImageReference.Digest("sha256:" + digest)
38+
39+
case .sha512:
40+
let hash = SHA512.hash(data: data)
41+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
42+
return try! ImageReference.Digest("sha512:" + digest)
43+
}
3044
}

0 commit comments

Comments
 (0)