Class: SimpleAES

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ SimpleAES

Returns a new instance of SimpleAES.



6
7
8
9
10
# File 'lib/simple_aes.rb', line 6

def initialize(args={})
  @key    = args.fetch(:key)
  @iv     = args.fetch(:iv)
  @cipher = args[:cypher] || 'AES-128-CBC'
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



4
5
6
# File 'lib/simple_aes.rb', line 4

def cipher
  @cipher
end

#ivObject (readonly)

Returns the value of attribute iv.



4
5
6
# File 'lib/simple_aes.rb', line 4

def iv
  @iv
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/simple_aes.rb', line 4

def key
  @key
end

Instance Method Details

#decrypt(encrypted_data) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/simple_aes.rb', line 12

def decrypt(encrypted_data)
  encrypted_data = Array(encrypted_data).pack('H*')
  aes = OpenSSL::Cipher::Cipher.new(cipher)
  aes.decrypt
  aes.key = key
  aes.iv = iv
  aes.update(encrypted_data) + aes.final
end

#encrypt(data) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/simple_aes.rb', line 21

def encrypt(data)
  aes = OpenSSL::Cipher::Cipher.new(cipher)
  aes.encrypt
  aes.key = key
  aes.iv = iv
  (aes.update(data) + aes.final).unpack('H*').first
end