Class: GPGME::Crypto
- Inherits:
-
Object
- Object
- GPGME::Crypto
- Defined in:
- lib/gpgme/crypto.rb
Overview
Different, independent methods providing the simplest possible API to execute crypto operations via GPG. All methods accept as options the same common options as GPGME::Ctx.new. Read the documentation for that class to know how to customize things further (like output stuff in ASCII armored format, for example).
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
Class Method Summary collapse
-
.method_missing(method, *args, &block) ⇒ Object
Allows calling of methods directly in the module without the need to create a new instance.
Instance Method Summary collapse
-
#clearsign(text, options = {}) ⇒ Object
Clearsigns an element.
-
#decrypt(cipher, options = {}) ⇒ GPGME::Data
Decrypts a previously encrypted element.
-
#detach_sign(text, options = {}) ⇒ Object
Creates a detached signature of an element.
-
#encrypt(plain, options = {}) ⇒ GPGME::Data
Encrypts an element.
-
#initialize(options = {}) ⇒ Crypto
constructor
A new instance of Crypto.
-
#sign(text, options = {}) ⇒ GPGME::Data
Creates a signature of a text.
-
#verify(sig, options = {}) ⇒ GPGME::Data
Verifies a previously signed element.
Constructor Details
#initialize(options = {}) ⇒ Crypto
Returns a new instance of Crypto.
18 19 20 |
# File 'lib/gpgme/crypto.rb', line 18 def initialize( = {}) @default_options = end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
16 17 18 |
# File 'lib/gpgme/crypto.rb', line 16 def @default_options end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
Allows calling of methods directly in the module without the need to create a new instance.
349 350 351 352 353 354 355 356 |
# File 'lib/gpgme/crypto.rb', line 349 def self.method_missing(method, *args, &block) if GPGME::Crypto.instance_methods(false).include?(method) crypto = GPGME::Crypto.new crypto.send method, *args, &block else super end end |
Instance Method Details
#clearsign(text, options = {}) ⇒ Object
Clearsigns an element
crypto.clearsign text,
Same functionality of #sign only doing clearsigns by default.
332 333 334 |
# File 'lib/gpgme/crypto.rb', line 332 def clearsign(text, = {}) sign text, .merge(:mode => GPGME::SIG_MODE_CLEAR) end |
#decrypt(cipher, options = {}) ⇒ GPGME::Data
Decrypts a previously encrypted element
crypto.decrypt cipher, , &block
Must have the appropiate key to be able to decrypt, of course. Returns a Data object which can then be read.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/gpgme/crypto.rb', line 166 def decrypt(cipher, = {}) = @default_options.merge plain_data = Data.new([:output]) cipher_data = Data.new(cipher) GPGME::Ctx.new() do |ctx| begin ctx.decrypt_verify(cipher_data, plain_data) rescue GPGME::Error::UnsupportedAlgorithm => exc exc.algorithm = ctx.decrypt_result.unsupported_algorithm raise exc rescue GPGME::Error::WrongKeyUsage => exc exc.key_usage = ctx.decrypt_result.wrong_key_usage raise exc end verify_result = ctx.verify_result if verify_result && block_given? verify_result.signatures.each do |signature| yield signature end end end plain_data.seek(0) plain_data end |
#detach_sign(text, options = {}) ⇒ Object
Creates a detached signature of an element
crypto.detach_sign text,
Same functionality of #sign only doing detached signs by default.
342 343 344 |
# File 'lib/gpgme/crypto.rb', line 342 def detach_sign(text, = {}) sign text, .merge(:mode => GPGME::SIG_MODE_DETACH) end |
#encrypt(plain, options = {}) ⇒ GPGME::Data
Encrypts an element
crypto.encrypt something,
Will return a Data element which can then be read.
Must have some key imported, look for Key.import to know how to import one, or the gpg documentation to know how to create one
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/gpgme/crypto.rb', line 79 def encrypt(plain, = {}) = @default_options.merge plain_data = Data.new(plain) cipher_data = Data.new([:output]) keys = Key.find(:public, [:recipients]) keys = nil if [:symmetric] flags = 0 flags |= GPGME::ENCRYPT_ALWAYS_TRUST if [:always_trust] GPGME::Ctx.new() do |ctx| begin if [:sign] if [:signers] # Optimization: reuse recipient Key objects if signers match # to avoid redundant key lookups signers = resolve_keys_for_signing([:signers], keys) ctx.add_signer(*signers) end ctx.encrypt_sign(keys, plain_data, cipher_data, flags) else ctx.encrypt(keys, plain_data, cipher_data, flags) end rescue GPGME::Error::UnusablePublicKey => exc exc.keys = ctx.encrypt_result.invalid_recipients raise exc rescue GPGME::Error::UnusableSecretKey => exc exc.keys = ctx.sign_result.invalid_signers raise exc end end cipher_data.seek(0) cipher_data end |
#sign(text, options = {}) ⇒ GPGME::Data
Creates a signature of a text
crypto.sign text,
Must have the appropiate key to be able to decrypt, of course. Returns a Data object which can then be read.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/gpgme/crypto.rb', line 237 def sign(text, = {}) = @default_options.merge plain = Data.new(text) output = Data.new([:output]) mode = [:mode] || GPGME::SIG_MODE_NORMAL GPGME::Ctx.new() do |ctx| if [:signer] signers = Key.find(:secret, [:signer], :sign) ctx.add_signer(*signers) end begin ctx.sign(plain, output, mode) rescue GPGME::Error::UnusableSecretKey => exc exc.keys = ctx.sign_result.invalid_signers raise exc end end output.seek(0) output end |
#verify(sig, options = {}) ⇒ GPGME::Data
Verifies a previously signed element
crypto.verify sig, , &block
Must have the proper keys available.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/gpgme/crypto.rb', line 306 def verify(sig, = {}) = @default_options.merge sig = Data.new(sig) signed_text = Data.new([:signed_text]) output = Data.new([:output]) unless [:signed_text] GPGME::Ctx.new() do |ctx| ctx.verify(sig, signed_text, output) ctx.verify_result.signatures.each do |signature| yield signature end end if output output.seek(0) output end end |