Sha256: 7f283b7606cd68de2ddec42511d5973e59c35869863c6f4e6aa9755bbfb2704c

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_relative "object_hash_rb/version"

require_relative "object_hash_rb/encode"
require_relative "object_hash_rb/cryptohash"

# Contains functions which encode the input into a standardized format,
# then cryptographically hash it.
module ObjectHash
  # rubocop:disable Lint/SelfAssignment
  Encode = Encode
  CryptoHash = CryptoHash
  # rubocop:enable Lint/SelfAssignment

  module_function

  # Encode the input into a standardized format,
  # then cryptographically hash it.
  # @param input: Any object that should be encoded.
  # @param algorithm: Either a string naming the algorithm to use, or a Digest object that can hash the string.
  #   To preview the output of encoding, use "passthrough".
  # @param replacer: An optional function called on objects before they are encoded.
  #   Use this to replace unencodable objects with Strings or Hashes.
  # @param unordered_objects: If true, objects will have sorted keys.
  #   If false, objects with different order in their keys will have different hashes.
  def hash(input, algorithm: "sha1", replacer: nil, unordered_objects: true)
    CryptoHash.perform_cryptohash(
      Encode::Encoder.new(
        replacer: replacer,
        unordered_objects: unordered_objects
      ).perform_encode(input),
      algorithm
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_hash_rb-0.1.3 lib/object_hash_rb.rb
object_hash_rb-0.1.2 lib/object_hash_rb.rb