|
15 | 15 | import Foundation |
16 | 16 | import HTTPTypes |
17 | 17 | import struct Crypto.SHA256 |
| 18 | +import struct Crypto.SHA512 |
18 | 19 |
|
19 | 20 | // 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. |
20 | 21 | extension HTTPField.Name { static let dockerContentDigest = Self("Docker-Content-Digest")! } |
21 | 22 |
|
22 | 23 | /// 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. |
24 | 27 | /// - 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 { |
26 | 32 | // 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 | + } |
30 | 44 | } |
0 commit comments