lib/blind_index.rb in blind_index-0.3.4 vs lib/blind_index.rb in blind_index-0.3.5

- old
+ new

@@ -9,20 +9,20 @@ class Error < StandardError; end class << self attr_accessor :default_options end - self.default_options = { - iterations: 10000, - algorithm: :pbkdf2_sha256, - insecure_key: false, - encode: true, - cost: {} - } + self.default_options = {} def self.generate_bidx(value, key:, **options) - options = default_options.merge(options) + options = { + iterations: 10000, + algorithm: :pbkdf2_sha256, + insecure_key: false, + encode: true, + cost: {} + }.merge(default_options).merge(options) # apply expression value = options[:expression].call(value) if options[:expression] unless value.nil? @@ -33,10 +33,15 @@ key = key.call if key.respond_to?(:call) raise BlindIndex::Error, "Missing key for blind index" unless key key = key.to_s unless options[:insecure_key] && algorithm == :pbkdf2_sha256 + # decode hex key + if key.encoding != Encoding::BINARY && key =~ /\A[0-9a-f]{64}\z/i + key = [key].pack("H*") + end + raise BlindIndex::Error, "Key must use binary encoding" if key.encoding != Encoding::BINARY raise BlindIndex::Error, "Key must be 32 bytes" if key.bytesize != 32 end # gist to compare algorithm results @@ -87,9 +92,14 @@ end else value end end + end + + def self.generate_key + require "securerandom" + SecureRandom.hex(32) end end ActiveSupport.on_load(:active_record) do require "blind_index/extensions"