Sha256: 9589b13e447a65daf971faca80a11a08ef53cd34a4c8ddd10e72790d6cfe31f3

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'openssl'
require 'tempfile'
require 'fileutils'

module Heirloom
  module Cipher
    class File

      def initialize(args)
        @config = args[:config]
        @aes = OpenSSL::Cipher::AES256.new(:CBC)
      end

      def encrypt_file(args)
        @file           = args[:file]
        @encrypted_file = Tempfile.new('archive.tar.gz.enc')
        secret          = args[:secret]
        iv              = @aes.random_iv

        @aes.encrypt
        @aes.iv = iv
        @aes.key = Digest::SHA256.hexdigest secret

        # Need to refactor to be less complex
        # Additionally tests to do fully cover logic
        ::File.open(@encrypted_file,'w') do |enc|
          enc << iv
          ::File.open(@file) do |f|
            loop do
              r = f.read(4096)
              break unless r
              enc << @aes.update(r)
            end
          end
          enc << @aes.final
        end

        replace_file
      end

      private

      def replace_file
        FileUtils.mv @encrypted_file.path, @file
        @encrypted_file.close!
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heirloom-0.9.0 lib/heirloom/cipher/file.rb