Class: ObjectHash
- Inherits:
-
Object
- Object
- ObjectHash
- Defined in:
- lib/objecthash.rb,
lib/objecthash/version.rb
Overview
A content hash algorithm which works across multiple encodings (JSON, Protobufs, etc)
Constant Summary collapse
- DEFAULT_HASH_ALGORITHM =
Default algorithm to use for computing object hashes
Digest::SHA256
- VERSION =
"1.0.1".freeze
Class Method Summary collapse
-
.digest(object, normalize: true) ⇒ Object
Compute a raw ObjectHash digest using the default algorithm.
-
.hexdigest(object, normalize: true) ⇒ Object
Compute an hex ObjectHash digest using the default algorithm.
Instance Method Summary collapse
-
#digest(object, normalize: true) ⇒ Object
Compute the ObjectHash of the given object.
-
#hexdigest(object, normalize: true) ⇒ Object
Compute the ObjectHash of the given object as hexadecimal.
-
#initialize(hash_algorithm = DEFAULT_HASH_ALGORITHM) ⇒ ObjectHash
constructor
A new instance of ObjectHash.
Constructor Details
#initialize(hash_algorithm = DEFAULT_HASH_ALGORITHM) ⇒ ObjectHash
Returns a new instance of ObjectHash.
24 25 26 |
# File 'lib/objecthash.rb', line 24 def initialize(hash_algorithm = DEFAULT_HASH_ALGORITHM) @hash_algorithm = hash_algorithm end |
Class Method Details
.digest(object, normalize: true) ⇒ Object
Compute a raw ObjectHash digest using the default algorithm
15 16 17 |
# File 'lib/objecthash.rb', line 15 def self.digest(object, normalize: true) new.digest(object, normalize: normalize) end |
.hexdigest(object, normalize: true) ⇒ Object
Compute an hex ObjectHash digest using the default algorithm
20 21 22 |
# File 'lib/objecthash.rb', line 20 def self.hexdigest(object, normalize: true) new.hexdigest(object, normalize: normalize) end |
Instance Method Details
#digest(object, normalize: true) ⇒ Object
Compute the ObjectHash of the given object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/objecthash.rb', line 29 def digest(object, normalize: true) case object when Array then obj_hash_list(object) when Hash then obj_hash_dict(object) when String then obj_hash_unicode(object, normalize) when Symbol then obj_hash_unicode(object.to_s, normalize) when Float then obj_hash_float(object) when Fixnum then obj_hash_int(object) when Set then obj_hash_set(object) when true, false then obj_hash_bool(object) when nil then hash_primitive("n", "") else raise TypeError, "unsupported: #{object.class}" end end |
#hexdigest(object, normalize: true) ⇒ Object
Compute the ObjectHash of the given object as hexadecimal
45 46 47 |
# File 'lib/objecthash.rb', line 45 def hexdigest(object, normalize: true) digest(object, normalize: normalize).unpack("H*").first end |