Sha256: eaab698f41b3e0f7ff27c9e70eb6c11b1fdb0b0917d9a28998cd16588e1668db

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# encrypts strings using supplied encryption settings
class Encrypt
  # encrypts a string with openssl
  def self.openssl(settings)
    require 'openssl'

    # setup the encryption parameters
    cipher = OpenSSL::Cipher.new('aes-256-gcm').encrypt
    cipher.key = settings[:key]
    cipher.iv = settings[:nonce]
    cipher.auth_data = settings.key?(:pw) ? settings[:pw] : ''

    # output the encryption and associated tag
    if settings[:ui] == :cli
      # output to file
      File.write("#{settings[:outdir]}encrypted.txt", cipher.update(settings[:file]) + cipher.final)
      File.write("#{settings[:outdir]}tag.txt", cipher.auth_tag)
      puts "Your encrypted.txt and associated tag.txt for this encryption have been generated in #{settings[:outdir]}."
    elsif settings[:ui] == :api
      # output to array
      [cipher.update(settings[:file]) + cipher.final, cipher.auth_tag]
    end
  end

  # encrypts a string with gpgme
  def self.gpgme(settings)
    require 'gpgme'

    # check if GPGHOME env was set
    puts "Environment variable 'GNUPGHOME' was not set. Files in #{ENV['HOME']}/.gnupg will be used for authentication." unless ENV['GNUPGHOME']

    # setup the encryption parameters
    crypto = GPGME::Crypto.new(armor: true, pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK)

    # output the encryption and associated tag
    if settings[:ui] == :cli
      # output to file
      File.write("#{settings[:outdir]}encrypted.txt", crypto.encrypt(settings[:file], symmetric: true, password: settings[:pw]).read)
      puts "Your encrypted.txt for this encryption have been generated in #{settings[:outdir]}."
    elsif settings[:ui] == :api
      # output to string
      crypto.encrypt(settings[:file], symmetric: true, password: settings[:pw]).read
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rapid-vaults-1.1.2 lib/rapid-vaults/encrypt.rb
rapid-vaults-1.1.1 lib/rapid-vaults/encrypt.rb