Class: Keyring::Encryptor::AES::Base

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

Direct Known Subclasses

AES128CBC, AES192CBC, AES256CBC

Class Method Summary collapse

Class Method Details

.build_cipherObject



5
6
7
# File 'lib/keyring/encryptor/aes.rb', line 5

def self.build_cipher
  OpenSSL::Cipher.new(cipher_name)
end

.decrypt(key, message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/keyring/encryptor/aes.rb', line 24

def self.decrypt(key, message)
  cipher = build_cipher
  cipher.decrypt

  message = Base64.strict_decode64(message)
  iv = message[0...cipher.iv_len]
  encrypted = message[cipher.iv_len..-1]

  cipher.iv = iv
  cipher.key = key
  cipher.update(encrypted) + cipher.final
end

.encrypt(key, message) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/keyring/encryptor/aes.rb', line 13

def self.encrypt(key, message)
  cipher = build_cipher
  cipher.encrypt
  iv = cipher.random_iv
  cipher.iv  = iv
  cipher.key = key
  encrypted = cipher.update(message) + cipher.final

  Base64.strict_encode64("#{iv}#{encrypted}")
end

.key_sizeObject



9
10
11
# File 'lib/keyring/encryptor/aes.rb', line 9

def self.key_size
  @key_size ||= build_cipher.key_len
end