@@ -24,7 +24,7 @@ func splitReference(_ reference: String) throws -> (String?, String) {
2424 // Hostname heuristic: contains a '.' or a ':', or is localhost
2525 if splits [ 0 ] != " localhost " , !splits[ 0 ] . contains ( " . " ) , !splits[ 0 ] . contains ( " : " ) { return ( nil , reference) }
2626
27- return ( String ( splits [ 0 ] ) , String ( splits [ 1 ] ) )
27+ return ( " \ (splits [ 0 ] ) " , " \ (splits [ 1 ] ) " )
2828}
2929
3030// Split the name into repository and tag parts
@@ -35,8 +35,8 @@ func parseName(_ name: String) throws -> (ImageReference.Repository, any ImageRe
3535 let digestSplit = name. split ( separator: " @ " , maxSplits: 1 , omittingEmptySubsequences: false )
3636 if digestSplit. count == 2 {
3737 return (
38- try ImageReference . Repository ( String ( digestSplit [ 0 ] ) ) ,
39- try ImageReference . Digest ( String ( digestSplit [ 1 ] ) )
38+ try ImageReference . Repository ( " \ (digestSplit [ 0 ] ) " ) ,
39+ try ImageReference . Digest ( " \ (digestSplit [ 1 ] ) " )
4040 )
4141 }
4242
@@ -51,8 +51,8 @@ func parseName(_ name: String) throws -> (ImageReference.Repository, any ImageRe
5151
5252 // assert splits == 2
5353 return (
54- try ImageReference . Repository ( String ( tagSplit [ 0 ] ) ) ,
55- try ImageReference . Tag ( String ( tagSplit [ 1 ] ) )
54+ try ImageReference . Repository ( " \ (tagSplit [ 0 ] ) " ) ,
55+ try ImageReference . Tag ( " \ (tagSplit [ 1 ] ) " )
5656 )
5757}
5858
@@ -214,45 +214,63 @@ extension ImageReference {
214214
215215 /// Digest identifies a specific blob by the hash of the blob's contents.
216216 public struct Digest : Reference , Sendable , Equatable , CustomStringConvertible , CustomDebugStringConvertible {
217+ public enum Algorithm : String , Sendable {
218+ case sha256 = " sha256 "
219+ case sha512 = " sha512 "
220+
221+ init ( fromString rawValue: String ) throws {
222+ guard let algorithm = Algorithm ( rawValue: rawValue) else {
223+ throw RegistryClientError . invalidDigestAlgorithm ( rawValue)
224+ }
225+ self = algorithm
226+ }
227+ }
228+
229+ var algorithm : Algorithm
217230 var value : String
218231
219232 public enum ValidationError : Error , Equatable {
220233 case emptyString
221234 case invalidReferenceFormat( String )
222- case tooLong( String )
223235 }
224236
225237 public init ( _ rawValue: String ) throws {
226238 guard rawValue. count > 0 else {
227239 throw ValidationError . emptyString
228240 }
229241
230- if rawValue. count > 7 + 64 {
231- throw ValidationError . tooLong ( rawValue)
242+ // https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#sha-256
243+ let sha256digest = /(sha256):([a-fA-F0-9]{64})/
244+ if let match = try sha256digest. wholeMatch ( in: rawValue) {
245+ algorithm = try Algorithm ( fromString: " \( match. 1 ) " )
246+ value = " \( match. 2 ) "
247+ return
232248 }
233249
234- // https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests
235- let regex = /sha256:[a-fA-F0-9]{64}/
236- if try regex. wholeMatch ( in: rawValue) == nil {
237- throw ValidationError . invalidReferenceFormat ( rawValue)
250+ // https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#sha-512
251+ let sha512digest = /(sha512):([a-fA-F0-9]{128})/
252+ if let match = try sha512digest. wholeMatch ( in: rawValue) {
253+ algorithm = try Algorithm ( fromString: " \( match. 1 ) " )
254+ value = " \( match. 2 ) "
255+ return
238256 }
239257
240- value = rawValue
258+ throw ValidationError . invalidReferenceFormat ( rawValue)
241259 }
242260
243261 public static func == ( lhs: Digest , rhs: Digest ) -> Bool {
244- lhs. value == rhs. value
262+ lhs. algorithm == rhs . algorithm && lhs . value == rhs. value
245263 }
246264
247265 public var separator : String = " @ "
248266
249267 public var description : String {
250- " \( value) "
268+ " \( algorithm ) : \( value) "
251269 }
252270
253271 /// Printable description in a form suitable for debugging.
254272 public var debugDescription : String {
255- " Digest( \( value) ) "
273+ " Digest( \( algorithm ) : \( value) ) "
256274 }
257275 }
258276}
0 commit comments