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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/edb/cryptography/aes_256_cbc.rb', line 49 def decrypt(source, new_file = true) ::EDB::Logger.log(:info, "Decrypting #{source}...") ciphered_content = File.read(source) raise "Cannot decrypt #{source}: It's empty" if ciphered_content.empty? ciphered_content_length = ciphered_content.length decipher = OpenSSL::Cipher.new('AES-256-CBC') decipher.decrypt decipher.key = hash_key(::EDB.opts[:CRYPTOGRAPHY][:AES_256_CBC][:secret]) decipher.iv = iv = ciphered_content.slice!(ciphered_content_length - 16, ciphered_content_length) new_source = new_file ? "#{source}.dec" : source File.open(new_source, 'wb') do |file| deciphered_content = decipher.update(ciphered_content) + decipher.final file.write(deciphered_content) end end |
.encrypt(source) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/edb/cryptography/aes_256_cbc.rb', line 31 def encrypt(source) ::EDB::Logger.log(:info, "Encrypting #{source}...") cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher.encrypt cipher.key = hash_key(::EDB.opts[:CRYPTOGRAPHY][:AES_256_CBC][:secret]) cipher.iv = iv = cipher.random_iv 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 ciphered_content << iv file.write(ciphered_content) end end |