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 when there's neither a configured hash_salt nor a Rails app with a secret_key_base. It's public, so these hashes can be matched to a list of candidate values.

"l0g5t0p"

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)


25
26
27
# File 'lib/log_struct/hash_utils.rb', line 25

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)


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

def hash_value(value)
  length = LogStruct.config.filters.hash_length
  OpenSSL::HMAC.hexdigest("SHA256", hash_key, value)[0...length] || "error"
end

.reset! ⇒ void

This method returns an undefined value.

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



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

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