def test_should_allow_hex_string_key(): branca = Branca( key="73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT" assert branca.decode(token) == b"Hello world!" assert branca.timestamp(token) == 123206400
def test_decode_hello_world_with_max_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "89i7YCwu5tWAJNHUDdmIqhzOi5hVHOd4afjZcGMcVmM4enl4yeLiDyYv41eMkNmTX6IwYEFErCSqr" assert branca.decode(token) == b"Hello world!" assert branca.timestamp(token) == 4294967295
def test_should_get_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "1jJDJOEeG2FutA8g7NAOHK4Mh5RIE8jtbXd63uYbrFDSR06dtQl9o2gZYhBa36nZHXVfiGFz" assert branca.timestamp(token) == 123206400
def test_decode_hello_world_with_zero_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z" assert branca.decode(token) == b"Hello world!" assert branca.timestamp(token) == 0
def test_decode_eight_nul_bytes_with_nov27_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "1jJDJOEjuwVb9Csz1Ypw1KBWSkr0YDpeBeJN6NzJWx1VgPLmcBhu2SbkpQ9JjZ3nfUf7Aytp" assert branca.decode(token) == b"\x00\x00\x00\x00\x00\x00\x00\x00" assert branca.timestamp(token) == 123206400
def test_decode_eight_nul_bytes_with_max_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "1jrx6DUu5q06oxykef2e2ZMyTcDRTQot9ZnwgifUtzAphGtjsxfbxXNhQyBEOGtpbkBgvIQx" assert branca.decode(token) == b"\x00\x00\x00\x00\x00\x00\x00\x00" assert branca.timestamp(token) == 4294967295
def test_decode_eight_nul_bytes_with_zero_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "1jIBheHbDdkCDFQmtgw4RUZeQoOJgGwTFJSpwOAk3XYpJJr52DEpILLmmwYl4tjdSbbNqcF1" assert branca.decode(token) == b"\x00\x00\x00\x00\x00\x00\x00\x00" assert branca.timestamp(token) == 0
def test_decode_hello_world_with_nov27_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT" assert branca.decode(token) == b"Hello world!" assert branca.timestamp(token) == 123206400
def test_decode_non_utf8_payload(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "K9u6d0zjXp8RXNUGDyXAsB9AtPo60CD3xxQ2ulL8aQoTzXbvockRff0y1eXoHm" assert branca.decode(token) == b"\x80" assert branca.timestamp(token) == 123206400
def test_decode_empty_payload(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) token = "4sfD0vPFhIif8cy4nB3BQkHeJqkOkDvinI4zIhMjYX4YXZU5WIq9ycCVjGzB5" assert branca.decode(token) == b"" assert branca.timestamp(token) == 0
def from_ciphertext(cls, key: Key, ciphertext: str) -> "Token": assert ciphertext branca = Branca(key.data) try: timestamp = branca.timestamp(ciphertext) except (struct.error, ValueError): return cls(key, None, None) try: token_path = Path(branca.decode(ciphertext).decode("UTF-8")) except RuntimeError: return cls(key, None, timestamp) return cls(key, token_path, timestamp, ciphertext)