module BlsmVdCore::EncryptedId::ClassMethods

Public Instance Methods

decrypt(key, id) click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 51
def decrypt(key, id)
  c = OpenSSL::Cipher::Cipher.new(CIPHER_NAME).decrypt
  c.iv = CIPHER_IV
  c.key = key
  c.update([id].pack('H*')) + c.final
end
encrypt(key, id) click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 58
def encrypt(key, id)
  c = OpenSSL::Cipher::Cipher.new(CIPHER_NAME).encrypt
  c.iv = CIPHER_IV
  c.key = key
  (c.update("#{id}") + c.final).unpack('H*')[0]
end
encrypted_id(options = {}) click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 14
def encrypted_id(options = {})
  cattr_accessor :encrypted_id_key
  self.encrypted_id_key = Digest::SHA256.digest(options[:key] || encrypted_id_default_key)
  self.define_singleton_method(:find_single, lambda { puts "foo" })
end
encrypted_id_default_key() click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 47
def encrypted_id_default_key
  name
end
find(*args) click to toggle source
Calls superclass method
# File lib/blsm-vd-core/model/encrypted_id.rb, line 20
def find(*args)
  scope = args.slice!(0)
  options = args.slice!(0) || {}
  if !(scope.is_a? Symbol) && has_encrypted_id? && !options[:no_encrypted_id]
    begin
      scope =
          if scope.is_a? Array
            scope.map! { |encrypted_id| decrypt(encrypted_id_key, encrypted_id) }
          else
            decrypt(encrypted_id_key, "#{scope}")
          end
    rescue OpenSSL::Cipher::CipherError
      raise ActiveRecord::RecordNotFound.new("Could not decrypt ID #{args[0]}")
    end
  end
  options.delete(:no_encrypted_id)
  super(scope)
end
find_by_encrypted_id(*args) click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 39
def find_by_encrypted_id(*args)
  self.find(*args)
end
has_encrypted_id?() click to toggle source
# File lib/blsm-vd-core/model/encrypted_id.rb, line 43
def has_encrypted_id?
  true
end