Class: DockerDistribution::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_distribution/digest.rb

Constant Summary collapse

DEFAULT_ALGORITHM =
"sha256"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest_string) ⇒ Digest

Returns a new instance of Digest.



18
19
20
21
# File 'lib/docker_distribution/digest.rb', line 18

def initialize(digest_string)
  @digest_string = digest_string
  @algorithm_type, @encoded = digest_string.index(":") ? digest_string.split(":") : [DEFAULT_ALGORITHM, digest_string]
end

Instance Attribute Details

#algorithm_typeObject

Returns the value of attribute algorithm_type.



8
9
10
# File 'lib/docker_distribution/digest.rb', line 8

def algorithm_type
  @algorithm_type
end

#digest_stringObject

Returns the value of attribute digest_string.



8
9
10
# File 'lib/docker_distribution/digest.rb', line 8

def digest_string
  @digest_string
end

#encodedObject

Returns the value of attribute encoded.



8
9
10
# File 'lib/docker_distribution/digest.rb', line 8

def encoded
  @encoded
end

Class Method Details

.parse!(digest_string) ⇒ Object

Parse parses digest_string and returns the validated digest object. An error will

be raised if the format is invalid.


12
13
14
15
16
# File 'lib/docker_distribution/digest.rb', line 12

def self.parse!(digest_string)
  dgst = new(digest_string)
  dgst.validate!
  dgst
end

Instance Method Details

#algorithmObject

rubocop:enable Metrics/AbcSize



44
45
46
47
48
# File 'lib/docker_distribution/digest.rb', line 44

def algorithm
  send(:Digest, algorithm_type.upcase).new
rescue NameError, LoadError
  raise DigestUnsupported
end

#digestObject



23
24
25
# File 'lib/docker_distribution/digest.rb', line 23

def digest
  [algorithm_type, encoded].compact.join(":")
end

#to_sObject



27
28
29
# File 'lib/docker_distribution/digest.rb', line 27

def to_s
  digest
end

#validate!Object

rubocop:disable Metrics/AbcSize



32
33
34
35
36
37
38
39
40
41
# File 'lib/docker_distribution/digest.rb', line 32

def validate!
  index = digest_string.index(":")
  raise DigestInvalidFormat if index.nil? || index + 1 == digest_string.length

  raise DigestInvalidLength if algorithm.block_length != encoded.length

  return true if Regexp.anchored_encoded_regexp(algorithm_type.upcase).match?(encoded)

  raise DigestInvalidFormat
end