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.
347 348 349 350 351 352 353 354 |
# File 'lib/gpgme/crypto.rb', line 347 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.
330 331 332 |
# File 'lib/gpgme/crypto.rb', line 330 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.
164 165 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 |
# File 'lib/gpgme/crypto.rb', line 164 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.
340 341 342 |
# File 'lib/gpgme/crypto.rb', line 340 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 |
# 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] signers = Key.find(:public, [:signers], :sign) 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.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/gpgme/crypto.rb', line 235 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.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/gpgme/crypto.rb', line 304 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 |