Module: LogStruct::HashUtils

Extended by:
T::Sig
Defined in:
lib/log_struct/hash_utils.rb

Overview

Utility module for hashing sensitive data

Constant Summary collapse

FALLBACK_KEY =

The key without a hash_salt, before the Rails app has its secret_key_base. A fixed key would be public, and anyone could match its hashes to a list of candidate emails; these hashes differ between processes.

T.let(::SecureRandom.random_bytes(32).freeze, String)

Class Method Summary collapse

Class Method Details

.hash_key ⇒ String

The configured hash_salt, or a key derived from the app's secret_key_base

Returns:

  • (String)


32
33
34
# File 'lib/log_struct/hash_utils.rb', line 32

def hash_key
  LogStruct.config.filters.hash_salt || derived_key || FALLBACK_KEY
end

.hash_value(value) ⇒ String

An HMAC-SHA256 of the value, so lines about the same email can be traced without logging it

Parameters:

  • value (String)

Returns:

  • (String)


19
20
21
22
23
24
25
26
27
28
# File 'lib/log_struct/hash_utils.rb', line 19

def hash_value(value)
  length = LogStruct.config.filters.hash_length
  max_bytes = LogStruct.config.limits.max_string_bytes
  input = if value.bytesize > max_bytes
    value.byteslice(0, max_bytes).to_s.b + "\0".b + value.bytesize.to_s.b
  else
    value
  end
  OpenSSL::HMAC.hexdigest("SHA256", hash_key, input)[0...length] || "error"
end

.reset! ⇒ void

This method returns an undefined value.

Forget the derived key (for tests that change the Rails app)



38
39
40
# File 'lib/log_struct/hash_utils.rb', line 38

def reset!
  @derived_key = T.let(nil, T.nilable(String))
end