Module: EncryptedId

Defined in:
lib/encrypted_id.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

CIPHER_NAME =
'aes-256-cbc'
CIPHER_IV =
['1e5673b2572af26a8364a50af84c7d2a'].pack('H*')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt(key, id) ⇒ Object



17
18
19
20
21
22
# File 'lib/encrypted_id.rb', line 17

def self.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) ⇒ Object



24
25
26
27
28
29
# File 'lib/encrypted_id.rb', line 24

def self.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

Instance Method Details

#encrypted_id(options = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/encrypted_id.rb', line 9

def encrypted_id(options = {})
  extend ClassMethods
  include InstanceMethods
  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