Module: EDB::Cryptography::AES_256_CBC

Defined in:
lib/edb/cryptography/aes_256_cbc.rb

Class Method Summary collapse

Class Method Details

.decrypt(source, new_file = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/edb/cryptography/aes_256_cbc.rb', line 46

def decrypt(source, new_file = true)
  ::EDB::Logger.log(:info, "Decrypting #{source}...")

  decipher = OpenSSL::Cipher.new('AES-256-CBC')
  decipher.decrypt
  decipher.key = ::EDB.opts[:CRYPTOGRAPHY][:AES_256_CBC][:secret]

  contents = File.read(source)
  raise "Cannot decrypt #{source}: It's empty" if contents.empty?

  new_source = new_file ? "#{source}.dec" : source
  File.open(new_source, 'wb') do |file|
    deciphered_content = decipher.update(contents) + decipher.final
    file.write(deciphered_content)
  end
end

.encrypt(source) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/edb/cryptography/aes_256_cbc.rb', line 30

def encrypt(source)
  ::EDB::Logger.log(:info, "Encrypting #{source}...")

  cipher = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.encrypt
  cipher.key = ::EDB.opts[:CRYPTOGRAPHY][:AES_256_CBC][:secret]

  contents = File.read(source)
  raise "Cannot encrypt #{source}: It's empty" if contents.empty?

  File.open(source, 'wb') do |file|
    ciphered_content = cipher.update(contents) + cipher.final
    file.write(ciphered_content)
  end
end