Class: Decidim::FingerprintCalculator

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/fingerprint_calculator.rb

Overview

This class will generate a unique fingerprint given an arbitrarily deep hash, ensuring that the same fingerprint will be generated regardless of ordering and whether keys are symbols or strings.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ FingerprintCalculator

Public: Initializes the class with a source data to be fingerprinted.


12
13
14
# File 'decidim-core/lib/decidim/fingerprint_calculator.rb', line 12

def initialize(data)
  @data = data
end

Instance Method Details

#sourceObject

Public: Returns the fingerprint source before hashing, so that it can be inspected by the user.

Returns a String with the JSON representation of the normalized data.


27
28
29
# File 'decidim-core/lib/decidim/fingerprint_calculator.rb', line 27

def source
  @source ||= JSON.generate(sort_hash(@data))
end

#valueObject

Public: Generates a fingerprint hash.

Returns a String with the fingerprint.


19
20
21
# File 'decidim-core/lib/decidim/fingerprint_calculator.rb', line 19

def value
  @value ||= Digest::SHA256.hexdigest(source)
end