Skip to content

Commit f37dad9

Browse files
committed
ContainerRegistry: Add sha512 digest
1 parent e80dbcc commit f37dad9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Sources/ContainerRegistry/RegistryClient+Digest.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@
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.
2324
/// - Parameter data: Blob of data to digest.
2425
/// - Returns: The blob's digest, in the format expected by the distribution protocol.
25-
public func digest(of data: any DataProtocol) -> ImageReference.Digest {
26+
public func digest(of data: any DataProtocol, algorithm: ImageReference.Digest.Algorithm = .sha256) -> ImageReference.Digest {
2627
// 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)
28+
switch algorithm {
29+
case .sha256:
30+
let hash = SHA256.hash(data: data)
31+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
32+
return try! ImageReference.Digest("sha256:" + digest)
33+
34+
case .sha512:
35+
let hash = SHA512.hash(data: data)
36+
let digest = hash.compactMap { String(format: "%02x", $0) }.joined()
37+
return try! ImageReference.Digest("sha512:" + digest)
38+
}
3039
}

0 commit comments

Comments
 (0)