Sha256: 2d82c8ffe4402ab7bd971d93fd332fc2439d391942d42a1a408061c3f4c12c54

Contents?: true

Size: 898 Bytes

Versions: 25

Compression:

Stored size: 898 Bytes

Contents

require 'digest/sha2'

module Linguist
  module SHA256
    # Public: Create deep nested digest of value object.
    #
    # Useful for object comparison.
    #
    # obj - Object to digest.
    #
    # Returns String hex digest
    def self.hexdigest(obj)
      digest = Digest::SHA256.new

      case obj
      when String, Symbol, Integer
        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
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
github-linguist-7.30.0 lib/linguist/sha256.rb
github-linguist-7.29.0 lib/linguist/sha256.rb
github-linguist-7.28.0 lib/linguist/sha256.rb
github-linguist-7.27.0 lib/linguist/sha256.rb
github-linguist-7.26.0 lib/linguist/sha256.rb
github-linguist-7.25.0 lib/linguist/sha256.rb
github-linguist-7.24.1 lib/linguist/sha256.rb
github-linguist-7.24.0 lib/linguist/sha256.rb
github-linguist-7.23.0 lib/linguist/sha256.rb
github-linguist-7.22.1 lib/linguist/sha256.rb
github-linguist-7.22.0 lib/linguist/sha256.rb
github-linguist-7.21.0 lib/linguist/sha256.rb
github-linguist-7.20.0 lib/linguist/sha256.rb
github-linguist-7.19.0 lib/linguist/sha256.rb
github-linguist-7.18.0 lib/linguist/sha256.rb
github-linguist-7.17.0 lib/linguist/sha256.rb
github-linguist-7.16.1 lib/linguist/sha256.rb
github-linguist-7.16.0 lib/linguist/sha256.rb
github-linguist-7.15.0 lib/linguist/sha256.rb
github-linguist-7.14.0 lib/linguist/sha256.rb