Class: Datadog::Core::Remote::Configuration::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/remote/configuration/digest.rb

Overview

Stores and validates different cryptographic hash functions

Defined Under Namespace

Classes: InvalidHashTypeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, hexdigest) ⇒ Digest

Returns a new instance of Digest.



57
58
59
60
# File 'lib/datadog/core/remote/configuration/digest.rb', line 57

def initialize(type, hexdigest)
  @type = type.to_sym
  @hexdigest = hexdigest
end

Instance Attribute Details

#hexdigestObject (readonly)

Returns the value of attribute hexdigest.



25
26
27
# File 'lib/datadog/core/remote/configuration/digest.rb', line 25

def hexdigest
  @hexdigest
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/datadog/core/remote/configuration/digest.rb', line 25

def type
  @type
end

Class Method Details

.hexdigest(type, data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/core/remote/configuration/digest.rb', line 28

def hexdigest(type, data)
  unless String === data
    # This class (Digest) passes +data+ to the Ruby standard
    # library Digest routines without validating its type.
    # The stdlib Digest requires a String, and the previous
    # implementation of this class that used StringIO
    # unconditionally read from +data+ without validating the
    # type. Meaning, passing +nil+ as +data+ has never worked.
    # It still doesn't work in the present implementation.
    # Flag the nil data now to get earlier diagnostics when
    # developing tests for example.
    raise ArgumentError, "Invalid type for data: #{data.class}: expected String"
  end

  d = case type
  when :sha256
    ::Digest::SHA256.new
  when :sha512
    ::Digest::SHA512.new
  else
    raise InvalidHashTypeError, type
  end

  d.update(data)

  d.hexdigest
end

Instance Method Details

#check(content) ⇒ Object



62
63
64
# File 'lib/datadog/core/remote/configuration/digest.rb', line 62

def check(content)
  content.hexdigest(@type) == hexdigest
end