Module: Kinko::BlindIndex

Extended by:
BlindIndex
Included in:
BlindIndex
Defined in:
lib/kinko/blind_index.rb

Overview

HMAC-SHA256 lookup digest for encrypted columns.

Constant Summary collapse

DIGEST =
"SHA256"
SEPARATOR =
":"
NORMALIZERS =
{
  none: ->(v) { v.to_s },
  downcase: ->(v) { v.to_s.downcase },
  strip: ->(v) { v.to_s.strip },
  downcase_strip: ->(v) { v.to_s.downcase.strip },
  unicode: ->(v) { v.to_s.unicode_normalize(:nfc) }
}.freeze

Instance Method Summary collapse

Instance Method Details

#compute(value, version: Kinko.config.key_version, normalize: :none) ⇒ Object



22
23
24
25
26
27
# File 'lib/kinko/blind_index.rb', line 22

def compute(value, version: Kinko.config.key_version, normalize: :none)
  Kinko.config.require!
  key = decode_key(Kinko.config.key_for(version))
  hex = OpenSSL::HMAC.hexdigest(DIGEST, key, normalize_with(normalize, value))
  "#{version}#{SEPARATOR}#{hex}"
end

#match?(value, stored, normalize: :none) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kinko/blind_index.rb', line 29

def match?(value, stored, normalize: :none)
  version, digest = split(stored)
  return false unless key = Kinko.config.keys[version]

  candidate = OpenSSL::HMAC.hexdigest(
    DIGEST,
    decode_key(key),
    normalize_with(normalize, value)
  )
  OpenSSL.fixed_length_secure_compare(digest, candidate)
rescue InvalidEnvelope, ArgumentError
  false
end

#version_of(stored) ⇒ Object



43
# File 'lib/kinko/blind_index.rb', line 43

def version_of(stored) = split(stored).first