We've also created a CryptedFileKeyring in the similar way. The difference between Win32CryptoKeyring and CryptedFileKeyring is that CryptedFileKeyring uses the AES algorithm provided by PyCrypto to encrypt/decrypt users' passwords. This results that CryptedFileKeyring need the user input their password in encryption/decryption. This may be annoying, so this keyring is not encouraged for daily use.
Both keyrings extend the BasicFileKeyring in the lib. BasicFileKeyring is the abstract base class for general file keyring which supports encryption/decryption. You can created a keyring with your encrypt/decrypt algorithms by easily extending BasicFileKeyring.
For example, here is the source code for the UncryptedFileKeyring of the lib.
class UncrpytedFileKeyring(BasicFileKeyring):
"""A simple filekeyring which dose not encrypt the password.
"""
def filename(self):
"""Return the filename of the password file. It should be
"keyring_password.cfg" for Windows, ".keyring_password" for other
platforms.
"""
import sys
if sys.platform in ['win32']:
return "keyring_password.cfg"
return ".keyring_password"
def encrypt(self, password):
"""Directly return the password itself.
"""
return password
def decrypt(self, password_encrypted):
"""Directly return encrypted password.
"""
return password_encrypted
def supported(self):
"""Applicable for all platforms, but do not recommend.
"""
return 0Since UncryptFileKeyring dose not encrypt the password, its implementation is simple. The BasicFileKeyring handle all file parsing/stroring affairs. Here we just need due with the encryption/decryption.Notice that there is a supported() method for the keyring. It is a new abstract method added for the KeyringBackend. Every keyring needs implement this method to tell if it is applicable for current environment.
We've also polish the code according to PEP 008. So some method names have been changed. Here is the new definition for the KeyringBackend.
class KeyringBackend():
"""The abstract base class of the keyring, every backend must implement
this interface.
"""
__metaclass__ = ABCMeta
@abstractmethod
def supported(self):
"""Return if this keyring supports current enviroment.
-1: not applicable
0: suitable
1: recommended
"""
return -1
@abstractmethod
def get_password(self, service, username):
"""Get password of the username for the service
"""
pass
@abstractmethod
def set_password(self, service, username, password):
"""Set password for the username of the service
"""
return -1For more information, please visit our repository.
4 comments: