File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed
Sources/ContainerRegistry Expand file tree Collapse file tree 1 file changed +13
-4
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.
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}
You can’t perform that action at this time.
0 commit comments