from .secret_properties import secret_properties class secret(metaclass = secret_properties): def __init__(self, token: str): if not self.__check(token): raise Exception("Token is not valid.") self.__token = token def clone(self): return secret(self.token) def build(hashed: bytes, salt: bytes): separator = secret.salt_separator result = secret(hashed.hex() + separator + salt.hex()) return result @property def token(self) -> str: return self.__token @property def hashed(self) -> bytes: properties = self.__class__ separator = properties.salt_separator splited = self.token.split(separator) return bytes.fromhex(splited[0]) @property def salt(self) -> bytes: properties = self.__class__ separator = properties.salt_separator splited = self.token.split(separator) return bytes.fromhex(splited[-1]) def __str__(self) -> str: return self.token def __check(self, token: str) -> bool: properties = self.__class__ separator = properties.salt_separator splited = token.split(separator) if len(splited) != 2: return False hashed = splited[0] salt = splited[1] if len(hashed) != properties.hash_hex_length: return False if len(salt) / 2 != properties.salt_length: return False return True