Sha256: 0ca0f168a1dd3f110eed154429ebdc0251ed84b86ceccec2fb017254f9ea68f5

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

class EncryptedKeystore
  require 'openssl'
  require 'base64'

  attr_accessor :file, :out, :key, :iv

  def self.encrypt(file: nil, out: nil)
    enc = new(file: file, out: out)
    enc.encrypt

    { key: enc.key, iv: enc.iv }
  end

  def self.decrypt(file: nil, out: nil, key: nil, iv: nil)
    enc = new(file: file, out: out, key: key, iv: iv)
    enc.decrypt
  end

  def initialize(file: nil, out: nil, key: nil, iv: nil)
    @file = file
    @out = out
    @key = key
    @iv = iv
  end

  def decrypt
    validate

    write(dec_cipher)
  end

  def encrypt
    validate(enc: true)

    write(enc_cipher)
  end

private

  def dec_cipher
    cipher = OpenSSL::Cipher.new('aes-256-cbc')
    cipher.decrypt
    cipher.key = Base64.decode64(@key)
    cipher.iv = Base64.decode64(@iv)
    cipher
  end

  def enc_cipher
    cipher = OpenSSL::Cipher.new('aes-256-cbc')
    cipher.encrypt
    @key = Base64.encode64(cipher.random_key)
    @iv = Base64.encode64(cipher.random_iv)
    cipher
  end

  def write(cipher)
    validate

    buffer = +''
    File.open(@out, 'wb') do |outfile|
      File.open(@file, 'rb') do |infile|
        outfile << cipher.update(buffer) while infile.read(4096, buffer)
        outfile << cipher.final
      end
    end

    @out
  end

  def validate(enc: false)
    return true if !@file.nil? && !@out.nil? && (!enc || (!@key.nil? && !@iv.nil?))

    raise ArgumentError, 'Required arguments have not been provided.'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encrypted-keystore-0.0.5 lib/encrypted_keystore.rb