Class: Statsig::HashUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_utils.rb

Class Method Summary collapse

Class Method Details

.djb2(input_str) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/hash_utils.rb', line 4

def self.djb2(input_str)
  hash = 0
  input_str.each_char.each do |c|
    hash = (hash << 5) - hash + c.ord
    hash &= hash
  end
  hash &= 0xFFFFFFFF # Convert to unsigned 32-bit integer
  return hash.to_s
end

.djb2ForHash(input_hash) ⇒ Object



14
15
16
# File 'lib/hash_utils.rb', line 14

def self.djb2ForHash(input_hash)
  return djb2(input_hash.to_json)
end

.md5(input_str) ⇒ Object



22
23
24
# File 'lib/hash_utils.rb', line 22

def self.md5(input_str)
  return Digest::MD5.base64digest(input_str)
end

.sha256(input_str) ⇒ Object



18
19
20
# File 'lib/hash_utils.rb', line 18

def self.sha256(input_str)
  return Digest::SHA256.base64digest(input_str)
end

.sortHash(input_hash) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/hash_utils.rb', line 26

def self.sortHash(input_hash)
  dictionary = input_hash.clone.sort_by { |key| key }.to_h;
  input_hash.each do |key, value|
    if value.is_a?(Hash)
      dictionary[key] = self.sortHash(value)
    end
  end
  return dictionary
end