%PDF- %PDF-
Direktori : /lib/python3/dist-packages/duplicity/__pycache__/ |
Current File : //lib/python3/dist-packages/duplicity/__pycache__/gpginterface.cpython-312.pyc |
� �2e�Y � � � d Z ddlZddlZddlZddlmZ ddlZdZdZdZ g d�Z dd d ddd d d�Zd dddd�Z G d� de � Z G d� de � Z G d� de � Z G d� de � Zd� Zedk( rddlZddlmZ ej. e� yy)a^ Interface to GNU Privacy Guard (GnuPG) !!! This was renamed to gpginterface.py. Please refer to duplicity's README for the reason. !!! gpginterface is a Python module to interface with GnuPG which based on GnuPGInterface by Frank J. Tobin. It concentrates on interacting with GnuPG via filehandles, providing access to control GnuPG via versatile and extensible means. This module is based on GnuPG::Interface, a Perl module by the same author. Normally, using this module will involve creating a GnuPG object, setting some options in it's 'options' data member (which is of type Options), creating some pipes to talk with GnuPG, and then calling the run() method, which will connect those pipes to the GnuPG process. run() returns a Process object, which contains the filehandles to talk to GnuPG with. Example code: >>> import gpginterface >>> >>> plaintext = b"Three blind mice" >>> passphrase = "This is the passphrase" >>> >>> gnupg = gpginterface.GnuPG() >>> gnupg.options.armor = 1 >>> gnupg.options.meta_interactive = 0 >>> gnupg.options.extra_args.append('--no-secmem-warning') >>> >>> # Normally we might specify something in >>> # gnupg.options.recipients, like >>> # gnupg.options.recipients = [ '0xABCD1234', 'bob@foo.bar' ] >>> # but since we're doing symmetric-only encryption, it's not needed. >>> # If you are doing standard, public-key encryption, using >>> # --encrypt, you will need to specify recipients before >>> # calling gnupg.run() >>> >>> # First we'll encrypt the test_text input symmetrically >>> p1 = gnupg.run(['--symmetric'], ... create_fhs=['stdin', 'stdout', 'passphrase']) >>> >>> ret = p1.handles['passphrase'].write(passphrase) >>> p1.handles['passphrase'].close() >>> >>> ret = p1.handles['stdin'].write(plaintext) >>> p1.handles['stdin'].close() >>> >>> ciphertext = p1.handles['stdout'].read() >>> p1.handles['stdout'].close() >>> >>> # process cleanup >>> p1.wait() >>> >>> # Now we'll decrypt what we just encrypted it, >>> # using the convience method to get the >>> # passphrase to GnuPG >>> gnupg.passphrase = passphrase >>> >>> p2 = gnupg.run(['--decrypt'], create_fhs=['stdin', 'stdout']) >>> >>> ret = p2.handles['stdin'].write(ciphertext) >>> p2.handles['stdin'].close() >>> >>> decrypted_plaintext = p2.handles['stdout'].read() >>> p2.handles['stdout'].close() >>> >>> # process cleanup >>> p2.wait() >>> >>> # Our decrypted plaintext: >>> decrypted_plaintext b'Three blind mice' >>> >>> # ...and see it's the same as what we orignally encrypted >>> assert decrypted_plaintext == plaintext, "GnuPG decrypted output does not match original input" >>> >>> >>> ################################################## >>> # Now let's trying using run()'s attach_fhs parameter >>> >>> # we're assuming we're running on a unix... >>> infp = open('/etc/manpaths', 'rb') >>> >>> p1 = gnupg.run(['--symmetric'], create_fhs=['stdout'], ... attach_fhs={'stdin': infp}) >>> >>> # GnuPG will read the stdin from /etc/motd >>> ciphertext = p1.handles['stdout'].read() >>> >>> # process cleanup >>> p1.wait() >>> >>> # Now let's run the output through GnuPG >>> # We'll write the output to a temporary file, >>> import tempfile >>> temp = tempfile.TemporaryFile() >>> >>> p2 = gnupg.run(['--decrypt'], create_fhs=['stdin'], ... attach_fhs={'stdout': temp}) >>> >>> # give GnuPG our encrypted stuff from the first run >>> ret = p2.handles['stdin'].write(ciphertext) >>> p2.handles['stdin'].close() >>> >>> # process cleanup >>> p2.wait() >>> >>> # rewind the tempfile and see what GnuPG gave us >>> ret = temp.seek(0) >>> decrypted_plaintext = temp.read() >>> >>> # compare what GnuPG decrypted with our original input >>> ret = infp.seek(0) >>> input_data = infp.read() >>> assert decrypted_plaintext == input_data, "GnuPG decrypted output does not match original input" To do things like public-key encryption, simply pass do something like: gnupg.passphrase = 'My passphrase' gnupg.options.recipients = [ 'bob@foobar.com' ] gnupg.run( ['--sign', '--encrypt'], create_fhs=..., attach_fhs=...) Here is an example of subclassing gpginterface.GnuPG, so that it has an encrypt_string() method that returns ciphertext. >>> import gpginterface >>> >>> class MyGnuPG(gpginterface.GnuPG): ... ... def __init__(self): ... super().__init__() ... self.setup_my_options() ... ... def setup_my_options(self): ... self.options.armor = 1 ... self.options.meta_interactive = 0 ... self.options.extra_args.append('--no-secmem-warning') ... ... def encrypt_string(self, string, recipients): ... gnupg.options.recipients = recipients # a list! ... ... proc = gnupg.run(['--encrypt'], create_fhs=['stdin', 'stdout']) ... ... proc.handles['stdin'].write(string) ... proc.handles['stdin'].close() ... ... output = proc.handles['stdout'].read() ... proc.handles['stdout'].close() ... ... proc.wait() ... return output ... >>> gnupg = MyGnuPG() >>> ciphertext = gnupg.encrypt_string(b"The secret", ['E477C232']) >>> >>> # just a small sanity test here for doctest >>> import types >>> assert isinstance(ciphertext, bytes), "What GnuPG gave back is not bytes!" Here is an example of generating a key: >>> import gpginterface >>> gnupg = gpginterface.GnuPG() >>> gnupg.options.meta_interactive = 0 >>> >>> # We will be creative and use the logger filehandle to capture >>> # what GnuPG says this time, instead stderr; no stdout to listen to, >>> # but we capture logger to surpress the dry-run command. >>> # We also have to capture stdout since otherwise doctest complains; >>> # Normally you can let stdout through when generating a key. >>> >>> proc = gnupg.run(['--gen-key'], create_fhs=['stdin', 'stdout', ... 'logger']) >>> >>> ret = proc.handles['stdin'].write(b'''Key-Type: DSA ... Key-Length: 1024 ... # We are only testing syntax this time, so dry-run ... %dry-run ... Subkey-Type: ELG-E ... Subkey-Length: 1024 ... Name-Real: Joe Tester ... Name-Comment: with stupid passphrase ... Name-Email: joe@foo.bar ... Expire-Date: 2y ... Passphrase: abc ... %pubring foo.pub ... %secring foo.sec ... ''') >>> >>> proc.handles['stdin'].close() >>> >>> report = proc.handles['logger'].read() >>> proc.handles['logger'].close() >>> >>> proc.wait() COPYRIGHT: Copyright (C) 2001 Frank J. Tobin, ftobin@neverending.org LICENSE: This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/copyleft/lesser.html � N)�logz&Frank J. Tobin, ftobin@neverending.orgz0.3.2z>$Id: GnuPGInterface.py,v 1.6 2009/06/06 17:35:19 loafman Exp $)�stdin�stdout�stderr�wb�rb�r�w)r r r � passphrase�command�logger�statusz--passphrase-fdz--logger-fdz--status-fdz--command-fd)r r r r c �0 � e Zd ZdZd� Zdd�Zd� Zd� Zd� Zy) �GnuPGa Class instances represent GnuPG. Instance attributes of a GnuPG object are: * call -- string to call GnuPG with. Defaults to "gpg" * passphrase -- Since it is a common operation to pass in a passphrase to GnuPG, and working with the passphrase filehandle mechanism directly can be mundane, if set, the passphrase attribute works in a special manner. If the passphrase attribute is set, and no passphrase file object is sent in to run(), then GnuPG instnace will take care of sending the passphrase to GnuPG, the executable instead of having the user sent it in manually. * options -- Object of type gpginterface.Options. Attribute-setting in options determines the command-line options used when calling GnuPG. c �>