File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed
Sources/ContainerRegistry Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change 1515import Foundation
1616import HTTPTypes
1717import 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.
2021extension 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( of data: any DataProtocol , algorithm : ImageReference . Digest . Algorithm = . sha256 ) -> ImageReference . Digest {
2629 // 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+ switch algorithm {
31+ case . sha256:
32+ let hash = SHA256 . hash ( data: data)
33+ let digest = hash. compactMap { String ( format: " %02x " , $0) } . joined ( )
34+ return try ! ImageReference . Digest ( " sha256: " + digest)
35+
36+ case . sha512:
37+ let hash = SHA512 . hash ( data: data)
38+ let digest = hash. compactMap { String ( format: " %02x " , $0) } . joined ( )
39+ return try ! ImageReference . Digest ( " sha512: " + digest)
40+ }
3041}
You can’t perform that action at this time.
0 commit comments