| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import os
- from .secret_coder import secret_coder
- class code_key_manager:
- """
- This class is responsible for handling users crypto keys. It could decrypt
- code to use it in other secrets, and recrypt it to use other password.
- """
- def __init__(self, password: str, key: str) -> None:
- """
- Parameters:
- password (str): Password of key, used to decrypt it
- key (str): Encrypted key to handle it
- """
- self.__coder = secret_coder(password)
- self.__key = key
- @property
- def encrypted(self) -> str:
- """ This return key in encrypted form. """
- return self.__key
- @property
- def decrypted(self) -> str:
- """ This return decrypted key. """
- return self.__coder.decrypt(self.__key)
- def recrypt(self, new_password: str) -> object:
- """
- This return new instance of this class, but with new password, and
- key which use this new password to encrypt themself.
- Parameters:
- new_password (str): New password to recrypt key
- Returns:
- (code_key_manager): New key handler witn new password
- """
-
- coder = secret_coder(new_password)
- encoded = coder.encrypt(self.decrypted)
- return code_key_manager(new_password, encoded)
- class code_key_generator:
- """
- This class generate new random crypto key to use it for secrets
- encryption.
- """
- def __new__(cls, password: str) -> str:
- """
- This create new encryption random crypto key, which use given
- password to encrypt itself.
- Parameters:
- password (str): Password to encrypt new crypto key
- Returns:
- (str): New crypto key encrypted with given password
- """
- coder = secret_coder(password)
- key = cls.__random_key(cls)
- return coder.encrypt(key)
- def __random_key_size(cls) -> int:
- """
- This return number of bytes in random key.
- Returns:
- (int): Lenght of random key
- """
- return 128
- def __random_key(cls) -> str:
- """
- This return random key as string. It is not encrypted, it is only
- bare string.
- Returns:
- (str): New random key
- """
- byte_key = os.urandom(cls.__random_key_size(cls))
- string_key = byte_key.hex()
- return string_key
- class code_key:
- """
- This class is generator for code key handler. When only password is
- given, then random key generator is created. If also crypted key is
- provided, then it create full handler.
- """
- def __new__(cls, **kwargs) -> str | code_key_manager:
- """
- This is commander. It get paramaters, and return ready generated
- encoded key, or crypto key handler.
- Parameters (**kwargs):
- password (str): Password used to encode crypto keys
- crypted_key (str): Encrypted key, which would be handled (optional)
- Returns:
- (str): New random key, when crypted key is not provided
- (code_key_manager): New handler, when key is provided
- """
- password = kwargs["password"]
-
- if not "crypted_key" in kwargs:
- return code_key_generator(password)
- key = kwargs["crypted_key"]
- return code_key_manager(password, key)
|