Class: Encrypt

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

Overview

encrypts strings using supplied encryption settings

Class Method Summary collapse

Class Method Details

.gpgme(settings) ⇒ Object

encrypts a string with gpgme



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rapid-vaults/encrypt.rb', line 26

def self.gpgme(settings)
  require 'gpgme'

  # 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('encrypted.txt', crypto.encrypt(settings[:file], symmetric: true, password: settings[:pw]).read)
    puts 'Your encrypted.txt for this encryption have been generated in your current directory.'
  elsif settings[:ui] == :api
    # output to string
    crypto.encrypt(settings[:file], symmetric: true, password: settings[:pw]).read
  end
end

.openssl(settings) ⇒ Object

encrypts a string with openssl



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

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('encrypted.txt', cipher.update(settings[:file]) + cipher.final)
    File.write('tag.txt', cipher.auth_tag)
    puts 'Your encrypted.txt and associated tag.txt for this encryption have been generated in your current directory.'
  elsif settings[:ui] == :api
    # output to array
    [cipher.update(settings[:file]) + cipher.final, cipher.auth_tag]
  end
end