| Server IP : 185.11.201.71 / Your IP : 192.168.7.18 Web Server : Apache/2.4.29 (Ubuntu) System : Linux tech-virtual-machine 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64 User : tech ( 1000) PHP Version : 7.4.28 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/lib/python2.7/dist-packages/keyrings/alt/ |
Upload File : |
from __future__ import absolute_import
import os
import abc
try:
from keyczar import keyczar
except ImportError:
pass
from keyring.backend import Crypter
from keyring import errors
def has_keyczar():
with errors.ExceptionRaisedContext() as exc:
keyczar.__name__
return not bool(exc)
class BaseCrypter(Crypter):
"""Base Keyczar keyset encryption and decryption.
The keyset initialisation is deferred until required.
"""
@abc.abstractproperty
def keyset_location(self):
"""Location for the main keyset that may be encrypted or not"""
pass
@abc.abstractproperty
def encrypting_keyset_location(self):
"""Location for the encrypting keyset.
Use None to indicate that the main keyset is not encrypted
"""
pass
@property
def crypter(self):
"""The actual keyczar crypter"""
if not hasattr(self, '_crypter'):
# initialise the Keyczar keysets
if not self.keyset_location:
raise ValueError('No encrypted keyset location!')
reader = keyczar.readers.CreateReader(self.keyset_location)
if self.encrypting_keyset_location:
encrypting_keyczar = keyczar.Crypter.Read(
self.encrypting_keyset_location)
reader = keyczar.readers.EncryptedReader(reader,
encrypting_keyczar)
self._crypter = keyczar.Crypter(reader)
return self._crypter
def encrypt(self, value):
"""Encrypt the value.
"""
if not value:
return ''
return self.crypter.Encrypt(value)
def decrypt(self, value):
"""Decrypt the value.
"""
if not value:
return ''
return self.crypter.Decrypt(value)
class Crypter(BaseCrypter):
"""A Keyczar crypter using locations specified in the constructor
"""
def __init__(self, keyset_location, encrypting_keyset_location=None):
self._keyset_location = keyset_location
self._encrypting_keyset_location = encrypting_keyset_location
@property
def keyset_location(self):
return self._keyset_location
@property
def encrypting_keyset_location(self):
return self._encrypting_keyset_location
class EnvironCrypter(BaseCrypter):
"""A Keyczar crypter using locations specified by environment vars
"""
KEYSET_ENV_VAR = 'KEYRING_KEYCZAR_ENCRYPTED_LOCATION'
ENC_KEYSET_ENV_VAR = 'KEYRING_KEYCZAR_ENCRYPTING_LOCATION'
@property
def keyset_location(self):
val = os.environ.get(self.KEYSET_ENV_VAR)
if not val:
raise ValueError('%s environment value not set' %
self.KEYSET_ENV_VAR)
return val
@property
def encrypting_keyset_location(self):
return os.environ.get(self.ENC_KEYSET_ENV_VAR)