Class: OpenPGP::Engine::GnuPG
- Inherits:
-
OpenPGP::Engine
- Object
- OpenPGP::Engine
- OpenPGP::Engine::GnuPG
- Defined in:
- lib/openpgp/engine/gnupg.rb
Overview
GNU Privacy Guard (GnuPG) wrapper.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- OPTIONS =
{ :batch => true, :quiet => true, :no_verbose => true, :no_tty => true, :no_permission_warning => true, :no_random_seed_file => true, }
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#available? ⇒ Boolean
Determines if GnuPG is available.
-
#clearsign ⇒ Object
Makes a clear text OpenPGP signature.
-
#decrypt(ciphertext, options = {}) ⇒ String
Decrypts the given ciphertext using the specified key ID.
- #delete_secret_and_public_key(key_id) ⇒ void
-
#detach_sign ⇒ Object
Makes a detached OpenPGP signature.
-
#encrypt(plaintext, options = {}) ⇒ String
Encrypts the given plaintext to the specified recipients.
-
#exec(command, options = {}, &block) ⇒ IO
Executes a GnuPG command, yielding the standard input and returning the standard output.
-
#exec3(command, options = {}, &block) ⇒ Array(IO, IO, IO)
Executes a GnuPG command, yielding and returning the standard input, output and error.
-
#exec4(command, options = {}, &block) ⇒ void
Executes a GnuPG command, yielding the process identifier as well as the standard input, output and error.
-
#export(key_id = nil, opts = {}) ⇒ Message
Exports a specified key from the GnuPG keyring.
-
#gen_key(info = {}) ⇒ Integer
Generates a new OpenPGP keypair and stores it GnuPG’s keyring.
-
#import ⇒ void
Imports a specified keyfile into the GnuPG keyring.
-
#initialize(options = {}) ⇒ GnuPG
constructor
A new instance of GnuPG.
- #key_fingerprint(key_id, opts = {}) ⇒ String
-
#list_keys ⇒ Array
Returns an array of key IDs/titles of the keys in the public keyring.
-
#sign ⇒ void
Makes an OpenPGP signature.
-
#sign_file(key_id, file, passphrase) ⇒ Object
Makes an OpenPGP signature.
-
#verify(key_id, file) ⇒ Object
Verifies an OpenPGP signature.
-
#version ⇒ String
Returns the GnuPG version number.
Methods inherited from OpenPGP::Engine
Constructor Details
Instance Attribute Details
#options ⇒ Hash{Symbol => Object}
28 29 30 |
# File 'lib/openpgp/engine/gnupg.rb', line 28 def @options end |
#where ⇒ String
25 26 27 |
# File 'lib/openpgp/engine/gnupg.rb', line 25 def where @where end |
Class Method Details
.available? ⇒ Boolean
11 12 13 |
# File 'lib/openpgp/engine/gnupg.rb', line 11 def self.available? self.new.available? end |
Instance Method Details
#available? ⇒ Boolean
Determines if GnuPG is available.
41 42 43 |
# File 'lib/openpgp/engine/gnupg.rb', line 41 def available? !!version end |
#clearsign ⇒ Object
Makes a clear text OpenPGP signature.
161 162 163 |
# File 'lib/openpgp/engine/gnupg.rb', line 161 def clearsign() # TODO end |
#decrypt(ciphertext, options = {}) ⇒ String
Decrypts the given ciphertext using the specified key ID.
141 142 143 |
# File 'lib/openpgp/engine/gnupg.rb', line 141 def decrypt(ciphertext, = {}) # TODO end |
#delete_secret_and_public_key(key_id) ⇒ void
This method returns an undefined value.
100 101 102 103 |
# File 'lib/openpgp/engine/gnupg.rb', line 100 def delete_secret_and_public_key(key_id) opts = {:batch => true} OpenPGP::Message.parse(exec([:delete_secret_and_public_key, key_fingerprint(key_id)], opts ).read) end |
#detach_sign ⇒ Object
Makes a detached OpenPGP signature.
167 168 169 |
# File 'lib/openpgp/engine/gnupg.rb', line 167 def detach_sign() # TODO end |
#encrypt(plaintext, options = {}) ⇒ String
Encrypts the given plaintext to the specified recipients.
131 132 133 |
# File 'lib/openpgp/engine/gnupg.rb', line 131 def encrypt(plaintext, = {}) # TODO end |
#exec(command, options = {}, &block) ⇒ IO
Executes a GnuPG command, yielding the standard input and returning the standard output.
184 185 186 187 188 189 190 191 192 |
# File 'lib/openpgp/engine/gnupg.rb', line 184 def exec(command, = {}, &block) #:yields: stdin exec4(command, ) do |pid, stdin, stdout, stderr| block.call(stdin) if block_given? stdin.close_write pid, status = Process.waitpid2(pid) raise Error, stderr.read.chomp if status.exitstatus.nonzero? stdout end end |
#exec3(command, options = {}, &block) ⇒ Array(IO, IO, IO)
Executes a GnuPG command, yielding and returning the standard input, output and error.
201 202 203 204 205 206 207 208 209 |
# File 'lib/openpgp/engine/gnupg.rb', line 201 def exec3(command, = {}, &block) #:yields: stdin, stdout, stderr exec4(command, ) do |pid, stdin, stdout, stderr| block.call(stdin, stdout, stderr) if block_given? stdin.close_write pid, status = Process.waitpid2(pid) raise Error, stderr.read.chomp if status.exitstatus.nonzero? [stdin, stdout, stderr] end end |
#exec4(command, options = {}, &block) ⇒ void
This method returns an undefined value.
Executes a GnuPG command, yielding the process identifier as well as the standard input, output and error.
218 219 220 221 222 |
# File 'lib/openpgp/engine/gnupg.rb', line 218 def exec4(command, = {}, &block) #:yields: pid, stdin, stdout, stderr require 'rubygems' require 'open4' block.call(*Open4.popen4(cmdline(command, ))) end |
#export(key_id = nil, opts = {}) ⇒ Message
Exports a specified key from the GnuPG keyring.
85 86 87 |
# File 'lib/openpgp/engine/gnupg.rb', line 85 def export(key_id = nil, opts = {}) OpenPGP::Message.parse(exec([:export, *[key_id].flatten], opts ).read) end |
#gen_key(info = {}) ⇒ Integer
Generates a new OpenPGP keypair and stores it GnuPG’s keyring.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openpgp/engine/gnupg.rb', line 58 def gen_key(info = {}) stdin, stdout, stderr = exec3(:gen_key) do |stdin, stdout, stderr| stdin.puts "Key-Type: #{info[:key_type]}" if info[:key_type] stdin.puts "Key-Length: #{info[:key_length]}" if info[:key_length] stdin.puts "Subkey-Type: #{info[:subkey_type]}" if info[:subkey_type] stdin.puts "Subkey-Length: #{info[:subkey_length]}" if info[:subkey_length] stdin.puts "Name-Real: #{info[:name]}" if info[:name] stdin.puts "Name-Comment: #{info[:comment]}" if info[:comment] stdin.puts "Name-Email: #{info[:email]}" if info[:email] stdin.puts "Expire-Date: #{info[:expire_date]}" if info[:expire_date] stdin.puts "Passphrase: #{info[:passphrase]}" if info[:passphrase] stdin.puts "%commit" end stderr.each_line do |line| if (line = line.chomp) =~ /^gpg: key ([0-9A-F]+) marked as ultimately trusted/ return $1.to_i(16) # the key ID end end return nil end |
#import ⇒ void
This method returns an undefined value.
Imports a specified keyfile into the GnuPG keyring.
93 94 95 |
# File 'lib/openpgp/engine/gnupg.rb', line 93 def import() # TODO end |
#key_fingerprint(key_id, opts = {}) ⇒ String
109 110 111 112 113 114 115 |
# File 'lib/openpgp/engine/gnupg.rb', line 109 def key_fingerprint(key_id, opts = {}) = exec([:fingerprint, *[key_id].flatten], opts ).read if =~ /Key fingerprint = (.*)\n/ return $1.delete(" ") end nil end |
#list_keys ⇒ Array
Returns an array of key IDs/titles of the keys in the public keyring.
121 122 123 |
# File 'lib/openpgp/engine/gnupg.rb', line 121 def list_keys() # TODO end |
#sign ⇒ void
This method returns an undefined value.
Makes an OpenPGP signature.
149 150 151 |
# File 'lib/openpgp/engine/gnupg.rb', line 149 def sign() # TODO end |
#sign_file(key_id, file, passphrase) ⇒ Object
Makes an OpenPGP signature.
155 156 157 |
# File 'lib/openpgp/engine/gnupg.rb', line 155 def sign_file(key_id, file, passphrase) OpenPGP::Message.parse(exec([:sign, file],{ :local_user => key_id, :passphrase => passphrase}).read) end |
#verify(key_id, file) ⇒ Object
Verifies an OpenPGP signature.
173 174 175 |
# File 'lib/openpgp/engine/gnupg.rb', line 173 def verify(key_id, file) OpenPGP::Message.parse(exec([:verify, file],{ :local_user => key_id}).read) end |
#version ⇒ String
Returns the GnuPG version number.
49 50 51 |
# File 'lib/openpgp/engine/gnupg.rb', line 49 def version exec(:version).readline =~ /^gpg \(GnuPG\) (.*)$/ ? $1 : nil end |