Sha256: c596203314e43612bb9c50923ff6cdf1fd8bc13d8c02fe622001f8d387cd48aa

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# dependencies
require "active_support"

# modules
require "blind_index/model"
require "blind_index/version"

module BlindIndex
  class Error < StandardError; end

  def self.generate_bidx(value, key:, iterations:, expression: nil, **options)
    key = key.call if key.respond_to?(:call)

    raise BlindIndex::Error, "Missing key for blind index" unless key

    # apply expression
    value = expression.call(value) if expression

    unless value.nil?
      # generate hash
      digest = OpenSSL::Digest::SHA256.new
      value = OpenSSL::PKCS5.pbkdf2_hmac(value.to_s, key, iterations, digest.digest_length, digest)

      # encode
      [value].pack("m")
    end
  end
end

ActiveSupport.on_load(:active_record) do
  require "blind_index/extensions"
  extend BlindIndex::Model

  if defined?(ActiveRecord::TableMetadata)
    ActiveRecord::TableMetadata.prepend(BlindIndex::Extensions::TableMetadata)
  else
    ActiveRecord::PredicateBuilder.singleton_class.prepend(BlindIndex::Extensions::PredicateBuilder)
  end

  unless ActiveRecord::VERSION::STRING.start_with?("5.1.")
    ActiveRecord::Validations::UniquenessValidator.prepend(BlindIndex::Extensions::UniquenessValidator)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blind_index-0.2.1 lib/blind_index.rb
blind_index-0.2.0 lib/blind_index.rb