Sha256: 0826222e441fd1383b0228a21f3ab691b6858f2dd5119004fc76cc4665dba3df

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require_relative "eon/version"
require "openssl"
require "base64"

module Eon
  def self.encrypt_file(target, destination)
    encoded_key = IO.read("eon_aes.key")
    cipher = OpenSSL::Cipher.new("aes-256-cbc")
    cipher.encrypt
    cipher.key = Base64.decode64(encoded_key)
    data = IO.read(target)
    encrypted = cipher.update(data)
    encrypted << cipher.final
    IO.write(destination, Base64.encode64(encrypted))
    puts "Successfully encrypted #{target} here: #{destination}"
  rescue StandardError
    puts "Error encountered"
  end

  def self.decrypt_file(target, destination)
    encoded_key = IO.read("eon_aes.key")
    cipher = OpenSSL::Cipher.new("aes-256-cbc")
    cipher.decrypt
    cipher.key = Base64.decode64(encoded_key)
    data = IO.read(target)
    data = Base64.decode64(data)
    decrypted = cipher.update(data)
    decrypted << cipher.final
    IO.write(destination, decrypted)
    puts "Successfully decrypted #{target} here: #{destination}"
  rescue StandardError
    puts "Error encountered"
  end

  def self.generate
    cypher = OpenSSL::Cipher.new("aes-256-cbc")
    cypher.encrypt
    key = Base64.encode64(cypher.random_key)
    IO.write("eon_aes.key", key)
    puts "Success"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eon_crypt-0.2.0 lib/eon.rb
eon_crypt-0.1.0 lib/eon.rb