Class: GPG::Runner

Inherits:
Object
  • Object
show all
Includes:
PGP::LogCapable
Defined in:
lib/pgp/gpg/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PGP::LogCapable

#log

Instance Attribute Details

#version_cacheObject

Returns the value of attribute version_cache.



7
8
9
# File 'lib/pgp/gpg/runner.rb', line 7

def version_cache
  @version_cache
end

Instance Method Details

#decrypt_file(path, data_output_path, passphrase = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pgp/gpg/runner.rb', line 71

def decrypt_file(path, data_output_path, passphrase=nil)
  passphrase ||= ''
  command_pieces = [
      'gpg',
      '--quiet',
      '--batch',
      pinentry_mode_command_options(passphrase),
      passphrase_command_options(passphrase),
      '--yes',
      '--ignore-mdc-error',
      '--output',
      "\"#{data_output_path}\"",
      '--decrypt',
      "\"#{path}\""
  ]
  command = command_pieces.reject(&:empty?).join(' ')
  run_gpg_silent_command(command)
end

#delete_private_key(fingerprint) ⇒ Object



45
46
47
# File 'lib/pgp/gpg/runner.rb', line 45

def delete_private_key(fingerprint)
  run_gpg_silent_command("gpg --quiet --batch --yes --delete-secret-key #{fingerprint}")
end

#delete_public_key(fingerprint) ⇒ Object



49
50
51
# File 'lib/pgp/gpg/runner.rb', line 49

def delete_public_key(fingerprint)
  run_gpg_silent_command("gpg --quiet --batch --yes --delete-key #{fingerprint}")
end

#encrypt_file(path, data_output_path, recipients) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/pgp/gpg/runner.rb', line 109

def encrypt_file(path, data_output_path, recipients)
  recipients_str = recipients
                       .map { |s| "--recipient \"#{s}\"" }
                       .join(' ')
  command = "gpg --quiet --batch --yes --output \"#{data_output_path}\" #{recipients_str} --trust-model always --encrypt \"#{path}\""
  run_gpg_silent_command(command)
end

#import_key_from_file(path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/pgp/gpg/runner.rb', line 53

def import_key_from_file(path)
  log("Import Key; path: #{path}; contents:\n#{File.read(path)}")
  command = "gpg --batch -v --import \"#{path}\""
  run(command) do |stdin, output, handle|
    extract_recipients(output)
  end
end

#read_private_key_fingerprintsObject



31
32
33
34
35
36
# File 'lib/pgp/gpg/runner.rb', line 31

def read_private_key_fingerprints
  run('gpg --quiet --list-secret-keys --fingerprint --keyid-format LONG') do |stdin, output, handle|
    return [] unless handle.value.success?
    extract_fingerprints(output)
  end
end

#read_private_key_recipientsObject



17
18
19
20
21
22
# File 'lib/pgp/gpg/runner.rb', line 17

def read_private_key_recipients
  run('gpg --quiet --list-secret-keys --fingerprint --keyid-format LONG') do |stdin, output, handle|
    return [] unless handle.value.success?
    extract_recipients(output)
  end
end

#read_public_key_fingerprintsObject



38
39
40
41
42
43
# File 'lib/pgp/gpg/runner.rb', line 38

def read_public_key_fingerprints
  run('gpg --quiet --list-keys --fingerprint --keyid-format LONG') do |stdin, output, handle|
    return [] unless handle.value.success?
    extract_fingerprints(output)
  end
end

#read_public_key_recipientsObject



24
25
26
27
28
29
# File 'lib/pgp/gpg/runner.rb', line 24

def read_public_key_recipients
  run('gpg --quiet --list-keys --fingerprint --keyid-format LONG') do |stdin, output, handle|
    return [] unless handle.value.success?
    extract_recipients(output)
  end
end

#sign_file(path, data_output_path, passphrase = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pgp/gpg/runner.rb', line 90

def sign_file(path, data_output_path, passphrase=nil)
  passphrase ||= ''
  command_pieces = [
      'gpg',
      '--quiet',
      '--batch',
      pinentry_mode_command_options(passphrase),
      passphrase_command_options(passphrase),
      '--yes',
      '--ignore-mdc-error',
      '--output',
      "\"#{data_output_path}\"",
      '--sign',
      "\"#{path}\""
  ]
  command = command_pieces.reject(&:empty?).join(' ')
  run_gpg_silent_command(command)
end

#verify_signature_file(path, data_output_path = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/pgp/gpg/runner.rb', line 61

def verify_signature_file(path, data_output_path=nil)
  if data_output_path.nil?
    log("Verify Signature; path: #{path}; contents:\n#{File.read(path)}")
    run_gpg_silent_command("gpg --quiet --batch --verify \"#{path}\"")
  else
    log("Verify Signature; path: #{path}; data_output_path: #{data_output_path}; contents:\n#{File.read(path)}")
    run_gpg_silent_command("gpg --quiet --batch --output \"#{data_output_path}\" \"#{path}\"")
  end
end

#version_defaultObject



9
10
11
12
13
14
15
# File 'lib/pgp/gpg/runner.rb', line 9

def version_default
  if self.version_cache.nil?
    self.version_cache = read_version('gpg --version', '')
  end

  self.version_cache
end