Class: HasObfuscatedId::Obfuscator

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/has_obfuscated_id.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Obfuscator

Returns a new instance of Obfuscator.



34
35
36
37
# File 'app/models/concerns/has_obfuscated_id.rb', line 34

def initialize(key)
  @cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  @cipher_key = Digest::SHA256.digest(key)
end

Instance Method Details

#decrypt(value) ⇒ Object



45
46
47
48
49
# File 'app/models/concerns/has_obfuscated_id.rb', line 45

def decrypt(value)
  c = @cipher.decrypt
  c.key = @cipher_key
  c.update(Base64.urlsafe_decode64(value.to_s)) + c.final rescue nil
end

#encrypt(value) ⇒ Object



39
40
41
42
43
# File 'app/models/concerns/has_obfuscated_id.rb', line 39

def encrypt(value)
  c = @cipher.encrypt
  c.key = @cipher_key
  Base64.urlsafe_encode64(c.update(value.to_s) + c.final) rescue nil
end