Recommended Python cryptographic module?

后端 未结 8 1179
余生分开走
余生分开走 2020-12-13 00:50

I\'ve been exploring what cryptographic modules are available to Python, and I\'ve found 3: ezPyCrypt, yawPyCrypt and KeyCzar (which actually supports a few languages, but P

8条回答
  •  猫巷女王i
    2020-12-13 01:01

    If you are in an environment which includes GnuPG and Python >= 2.4, then you could also consider a tool such as python-gnupg. (Disclaimer: I'm the maintainer of this project.) It leaves the heavy lifting to gpg and provides a fairly straightforward API.

    Overview of API:

    >>> import gnupg
    >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory')
    >>> gpg.list_keys()
    
    [{
      ...
      'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2',
      'keyid': '197D5DAC68F1AAB2',
      'length': '1024',
      'type': 'pub',
      'uids': ['', 'Gary Gross (A test user) ']},
     {
      ...
      'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A',
      'keyid': '0C5FEFA7A921FC4A',
      'length': '1024',
      ...
      'uids': ['', 'Danny Davis (A test user) ']}]
    >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A'])
    >>> str(encrypted)
    
    '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n
    \nhQIOA/6NHMDTXUwcEAf
    ...
    -----END PGP MESSAGE-----\n'
    >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret')
    >>> str(decrypted)
    'Hello, world!'
    >>> signed = gpg.sign("Goodbye, world!", passphrase='secret')
    >>> verified = verified = gpg.verify(str(signed))
    >>> print "Verified" if verified else "Not verified"
    
    'Verified' 
    

提交回复
热议问题