code_key.py 3.5 KB

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