Class: Ikarus::Crypt

Inherits:
Object
  • Object
show all
Defined in:
lib/ikarus/crypt.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Crypt

Returns a new instance of Crypt.



7
8
9
# File 'lib/ikarus/crypt.rb', line 7

def initialize(token)
  @key256 = token
end

Instance Method Details

#decrypt(encrypted_message) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ikarus/crypt.rb', line 27

def decrypt(encrypted_message)
  tokens = encrypted_message.split('|')
  crypt = Base64.decode64(tokens[0])
  iv = Base64.decode64(tokens[1])
  cipher = OpenSSL::Cipher.new('aes-256-cbc')
  cipher.decrypt()
  cipher.key = @key256
  cipher.iv = iv
  cipher.update(encrypted_message) + cipher.final
end

#encrypt(message, url_encode = true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ikarus/crypt.rb', line 11

def encrypt(message, url_encode = true)
  cipher = OpenSSL::Cipher.new('aes-128-cbc')
  cipher.encrypt()
  iv = cipher.random_iv

  cipher = OpenSSL::Cipher.new('aes-256-cbc')
  cipher.encrypt
  cipher.key = @key256
  cipher.iv = iv
  crypt = cipher.update(message)
  crypt << cipher.final

  encrypted = [Base64.encode64(crypt).strip(), Base64.encode64(iv).strip()].join('|').gsub("\n", '')
  url_encode ? CGI.escape(encrypted) : encrypted
end