Class: Cifrado::CryptoServices

Inherits:
Object
  • Object
show all
Defined in:
lib/cifrado/crypto_services.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CryptoServices

Returns a new instance of CryptoServices.



7
8
9
10
11
12
13
# File 'lib/cifrado/crypto_services.rb', line 7

def initialize(options = {})
  @options = options
  @gpg_binary = @options[:gpg_binary] || '/usr/bin/gpg'
  @gpg_extra_args = @options[:gpg_extra_args] || []
  @gpg_extra_args = @gpg_extra_args.concat %w(--batch --yes)
  @encrypt_name = @options[:encrypt_name] || false
end

Class Method Details

.encrypted?(file) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/cifrado/crypto_services.rb', line 52

def self.encrypted?(file)
  output = `/usr/bin/gpg --yes --batch --no-use-agent --list-packets #{file} 2>&1`
  Log.debug output
  if output.match(/AES256 encrypted|encrypted with\s.*\skey,\sID\s.*created/m).nil?
    return false
  end
  true
end

Instance Method Details

#decrypt(file, output) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cifrado/crypto_services.rb', line 61

def decrypt(file, output)
  unless file and File.exist?(file)
    raise ArgumentError.new "Invalid file #{file}"
  end

  if output.nil?
    raise ArgumentError.new "Invalid output file path"
  end

  raise ArgumentError.new("#{@gpg_binary} not found") unless File.exist?(@gpg_binary)

  Log.debug "Decrypting file #{file}..."
  
  if output != '-'
    @gpg_extra_args << "--output #{Shellwords.escape(output)}"
  end

  if @options[:passphrase]
    @gpg_extra_args << "--no-use-agent --passphrase #{@options[:passphrase]}"
  end
  
  cmd = "#{@gpg_binary} #{@gpg_extra_args.join(' ')} --decrypt #{Shellwords.escape(file)}"
  Log.debug "Decrypting with: #{cmd}"
  out = `#{cmd} 2>&1`
  
  if $? != 0
    raise "Failed to decrypt file #{file}\n#{out}"
  else
    if @options[:delete_source]
      File.delete file 
      Log.debug "Deleting encrypted file #{file}"
    end
  end

  Log.debug out
  output
end

#encrypt(file, output) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cifrado/crypto_services.rb', line 15

def encrypt(file, output)
  unless file and File.exist?(file)
    raise ArgumentError.new "Invalid file #{file}"
  end

  if output.nil?
    raise ArgumentError.new "Invalid output file path"
  end

  # so we can use --use-embedded-filename to recover the original
  # filename
  Log.debug "Setting GPG filename #{file} (Base64 URL encoded)"
  clean_path = Pathname.new(file).cleanpath.to_s
  @gpg_extra_args << "--set-filename '#{Base64.urlsafe_encode64(clean_path)}'"

  check_args
  raise ArgumentError.new("#{@gpg_binary} not found") unless File.exist?(@gpg_binary)

  Log.debug "Encrypting file #{file}..."
  if @encrypt_name and output != '-'
    Log.debug "Scrambling file name #{file}..."
    dir = File.dirname(output)
    #output = File.join dir, (Digest::SHA2.new << (output + SecureRandom.hex)).to_s
    output = File.join dir, (Digest::SHA2.new << file).to_s
  end
  
  if output != '-'
    @gpg_extra_args << ["--output #{Shellwords.escape(output)}"]
  end

  if @options[:type] == :asymmetric 
    asymmetric file, output
  else @options[:type] == :symmetric
    symmetric file, output
  end
end