Class: Decrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid-vaults/decrypt.rb

Overview

decrypts strings using supplied decryption settings

Class Method Summary collapse

Class Method Details

.gpgme(settings) ⇒ Object

decrypts a string with gpgme



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rapid-vaults/decrypt.rb', line 27

def self.gpgme(settings)
  require 'gpgme'

  # check if GPGHOME env was set
  puts "Environment variable 'GNUPGHOME' was not set. Files in #{Dir.home}/.gnupg will be used for authentication." unless ENV.fetch('GNUPGHOME', false)

  # setup the decryption parameters
  encrypted = GPGME::Data.new(settings[:file])
  crypto = GPGME::Crypto.new(armor: true, pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK)

  # output the decryption
  case settings[:ui]
  when :cli
    # output to file
    File.write("#{settings[:outdir]}decrypted.txt", crypto.decrypt(encrypted, password: settings[:pw]).read)
    puts "Your decrypted.txt has been written out to #{settings[:outdir]}."
  when :api
    # output to string
    crypto.decrypt(encrypted, password: settings[:pw]).read
  end
end

.openssl(settings) ⇒ Object

decrypts a string with openssl



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rapid-vaults/decrypt.rb', line 4

def self.openssl(settings)
  require 'openssl'

  # setup the decryption parameters
  decipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt
  decipher.key = settings[:key]
  decipher.iv = settings[:nonce]
  decipher.auth_tag = settings[:tag]
  decipher.auth_data = settings.key?(:pw) ? settings[:pw] : ''

  # output the decryption
  case settings[:ui]
  when :cli
    # output to file
    File.write("#{settings[:outdir]}decrypted.txt", decipher.update(settings[:file]) + decipher.final)
    puts "Your decrypted.txt has been written out to #{settings[:outdir]}."
  when :api
    # output to string
    decipher.update(settings[:file]) + decipher.final
  end
end