Class: Scl::AES

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

Instance Method Summary collapse

Constructor Details

#initialize(block_size = 256, block_cipher = :CBC) ⇒ AES

Returns a new instance of AES.



3
4
5
6
# File 'lib/scl/aes.rb', line 3

def initialize(block_size=256, block_cipher=:CBC)
  @block_cipher = block_cipher || :CBC
  @block_size = block_size || 256
end

Instance Method Details

#build_cipherObject



8
9
10
# File 'lib/scl/aes.rb', line 8

def build_cipher
  OpenSSL::Cipher::AES.new(@block_size, @block_cipher)
end

#decrypt(ciphertext, key, iv) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/scl/aes.rb', line 20

def decrypt(ciphertext, key, iv)
  block_cipher = build_cipher
  block_cipher.decrypt
  block_cipher.key = key
  block_cipher.iv  =  iv
  block_cipher.update( ciphertext ) + block_cipher.final
end

#encrypt(plaintext, key = nil, iv = nil) ⇒ Object



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

def encrypt(plaintext, key=nil, iv=nil)
  block_cipher = build_cipher
  block_cipher.encrypt
  block_cipher.key = key ||= block_cipher.random_key
  block_cipher.iv  = iv  ||= block_cipher.random_iv
  [block_cipher.update(plaintext) + block_cipher.final, key, iv]
end