Module: Linguist::SHA256

Defined in:
lib/linguist/sha256.rb

Class Method Summary collapse

Class Method Details

.hexdigest(obj) ⇒ Object

Public: Create deep nested digest of value object.

Useful for object comparison.

obj - Object to digest.

Returns String hex digest



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/linguist/sha256.rb', line 12

def self.hexdigest(obj)
  digest = Digest::SHA256.new

  case obj
  when String, Symbol, Integer, Float
    digest.update "#{obj.class}"
    digest.update "#{obj}"
  when TrueClass, FalseClass, NilClass
    digest.update "#{obj.class}"
  when Array
    digest.update "#{obj.class}"
    for e in obj
      digest.update(hexdigest(e))
    end
  when Hash
    digest.update "#{obj.class}"
    for e in obj.map { |(k, v)| hexdigest([k, v]) }.sort
      digest.update(e)
    end
  else
    raise TypeError, "can't convert #{obj.inspect} into String"
  end

  digest.hexdigest
end