import os class apikey: """ This class represents API key, and it is also creating new apikeys from device random numbers generator. Methods ------- get_raw_size() : int Return size of bytes which was loaded from device random number generator. get_size() : int Return lenght of the API key as string. get_prefix() : str Return prefix of the API keys. create() : apikey Create new random API key. export() : str Export API key as string. """ def __init__(self, content: str) -> None: """ It recreate API key object from key, which was previously exported to string. Raises ------ ValueError When API key lenght is not correct. Parameters ---------- content : str Preiviously exported API key string. """ if len(content) != self.get_size(): raise ValueError("API key lenght is not correct.") self.__content = content @staticmethod def get_raw_size() -> int: """ It return lenght of the random bytes in the API key. Returns ------- int Lenght of the random bytes in the API key. """ return 128 @staticmethod def get_size() -> int: """ It return lenght of the API key string, with prefix and random bytes converted to the hex string. Returns ------- int Lenght of the API key string. """ return len(apikey.get_prefix()) + apikey.get_raw_size() * 2 @staticmethod def get_prefix() -> str: """ It return API key prefix. Returns ------- str Prefix for the API key. """ return "cxtk_" @staticmethod def create() -> object: """ It create new API key from random number generator device. Returns ------- apikey New random API key. """ raw = os.urandom(apikey.get_raw_size()) key = apikey.get_prefix() + raw.hex() return apikey(key) def export(self) -> str: """ It export API key as string. Returns ------- str API key string content. """ return self.__content def __repr__(self) -> str: """ It return readable API key description (first 32 letters). Returns ------- str Readable API key description. """ return "API key: \"" + self.__content[0:32] + "...\"" def __str__(self) -> str: """ It return API key string content. Returns ------- str API key as string. """ return self.__content def __eq__(self, other) -> bool: """ It check that API keys are same. Returns ------- bool True when API keys are same. """ return self.__content == other.__content def __ne__(self, other) -> bool: """ It check that API keys are not same. Returns ------- bool True when API keys are not same. """ return self.__content != other.__content