code_key.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. from .secret_coder import secret_coder
  3. class code_key_manager:
  4. """
  5. This class is responsible for handling users crypto keys. It could decrypt
  6. code to use it in other secrets, and recrypt it to use other password.
  7. """
  8. def __init__(self, password: str, key: str) -> None:
  9. """
  10. Parameters:
  11. password (str): Password of key, used to decrypt it
  12. key (str): Encrypted key to handle it
  13. """
  14. self.__coder = secret_coder(password)
  15. self.__key = key
  16. @property
  17. def encrypted(self) -> str:
  18. """ This return key in encrypted form. """
  19. return self.__key
  20. @property
  21. def decrypted(self) -> str:
  22. """ This return decrypted key. """
  23. return self.__coder.decrypt(self.__key)
  24. def recrypt(self, new_password: str) -> object:
  25. """
  26. This return new instance of this class, but with new password, and
  27. key which use this new password to encrypt themself.
  28. Parameters:
  29. new_password (str): New password to recrypt key
  30. Returns:
  31. (code_key_manager): New key handler witn new password
  32. """
  33. coder = secret_coder(new_password)
  34. encoded = coder.encrypt(self.decrypted)
  35. return code_key_manager(new_password, encoded)
  36. class code_key_generator:
  37. """
  38. This class generate new random crypto key to use it for secrets
  39. encryption.
  40. """
  41. def __new__(cls, password: str) -> str:
  42. """
  43. This create new encryption random crypto key, which use given
  44. password to encrypt itself.
  45. Parameters:
  46. password (str): Password to encrypt new crypto key
  47. Returns:
  48. (str): New crypto key encrypted with given password
  49. """
  50. coder = secret_coder(password)
  51. key = cls.__random_key(cls)
  52. return coder.encrypt(key)
  53. def __random_key_size(cls) -> int:
  54. """
  55. This return number of bytes in random key.
  56. Returns:
  57. (int): Lenght of random key
  58. """
  59. return 128
  60. def __random_key(cls) -> str:
  61. """
  62. This return random key as string. It is not encrypted, it is only
  63. bare string.
  64. Returns:
  65. (str): New random key
  66. """
  67. byte_key = os.urandom(cls.__random_key_size(cls))
  68. string_key = byte_key.hex()
  69. return string_key
  70. class code_key:
  71. """
  72. This class is generator for code key handler. When only password is
  73. given, then random key generator is created. If also crypted key is
  74. provided, then it create full handler.
  75. """
  76. def __new__(cls, **kwargs) -> str | code_key_manager:
  77. """
  78. This is commander. It get paramaters, and return ready generated
  79. encoded key, or crypto key handler.
  80. Parameters (**kwargs):
  81. password (str): Password used to encode crypto keys
  82. crypted_key (str): Encrypted key, which would be handled (optional)
  83. Returns:
  84. (str): New random key, when crypted key is not provided
  85. (code_key_manager): New handler, when key is provided
  86. """
  87. password = kwargs["password"]
  88. if not "crypted_key" in kwargs:
  89. return code_key_generator(password)
  90. key = kwargs["crypted_key"]
  91. return code_key_manager(password, key)