apikey.py 847 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import os
  2. class apikey:
  3. """
  4. This create new random API key.
  5. """
  6. def __new__(cls, lenght: int = 128) -> str:
  7. """
  8. This create new random API key.
  9. lenght: int - Lenght os the api key (default: 128)
  10. return: str - API key
  11. """
  12. byte = cls.__get_random(lenght)
  13. string = cls.__to_hex(byte)
  14. return string
  15. def __to_hex(byte: bytes) -> str:
  16. """
  17. This convert random bytes to string.
  18. byte: bytes - Bytes array to convert
  19. return: str - Result as string
  20. """
  21. return byte.hex()
  22. def __get_random(lenght: int) -> bytes:
  23. """
  24. Get random count of hex chars from os.
  25. lenght: int - Lenght of the bytes
  26. return: bytes - Random bytes from os
  27. """
  28. return os.urandom(lenght)