def test_crockford(): decoded = anybase32.decode(CROCKFORD, anybase32.CROCKFORD) assert (decoded == SAMPLE_TEXT)
def test_zbase32(): decoded = anybase32.decode(ZBASE32, anybase32.ZBASE32) assert (decoded == SAMPLE_TEXT)
def test_crockford(): decoded = anybase32.decode(CROCKFORD, anybase32.CROCKFORD) assert decoded == SAMPLE_TEXT
from __future__ import print_function import anybase32 data = b"The **quick** brown fox jumps over the (lazy) dog!" print("Crockford modified:", anybase32.encode(data, anybase32.CROCKFORD_MODIFIED)) print("Crockford:", anybase32.encode(data, anybase32.CROCKFORD)) print("Zbase32:", anybase32.encode(data, anybase32.ZBASE32)) print("RFC 3548:", anybase32.encode(data, anybase32.RFC_3548)) print("RFC 2938:", anybase32.encode(data, anybase32.RFC_2938)) print("RFC 4648:", anybase32.encode(data, anybase32.RFC_4648)) print("Nightmare:", anybase32.encode(data, anybase32.NIGHTMARE)) enc = anybase32.encode(data, b"0123456789`~!@#$%^&*()-_=+[]{}\/") print("Arbitrary:", enc) dec = anybase32.decode(enc, b"0123456789`~!@#$%^&*()-_=+[]{}\/") print(dec) with open("tests/icon.png", "rb") as icon: icon_bytes = icon.read() icon = anybase32.encode(icon_bytes, anybase32.ZBASE32) while icon: if len(icon) > 77: print("{0}".format(icon[:77])) icon = icon[77:] else: print("{0}".format(icon)) icon = None
def test_crockford_modified(): decoded = anybase32.decode(CROCKFORD_MODIFIED, anybase32.CROCKFORD_MODIFIED) assert (decoded == SAMPLE_TEXT)
def test_binary(): with open("tests/icon.png", "rb") as icon_file: icon_bytes = icon_file.read() decoded = anybase32.decode(ICON_ZBASE32, anybase32.ZBASE32) assert (decoded == icon_bytes)
def test_crockford_modified(): decoded = anybase32.decode(CROCKFORD_MODIFIED, anybase32.CROCKFORD_MODIFIED) assert decoded == SAMPLE_TEXT
def test_arbitrary(): decoded = anybase32.decode(ARBITRARY, ARBITRARY_ALPHABET) assert (decoded == SAMPLE_TEXT)
def test_arbitrary(): decoded = anybase32.decode(ARBITRARY, ARBITRARY_ALPHABET) assert decoded == SAMPLE_TEXT
def test_too_long_alphabet(): with pytest.raises(ValueError) as e: anybase32.decode(RFC_3548, TOO_LONG_ALPHABET) assert "Invalid base32 alphabet" in str(e)
def test_rfc_46488(): decoded = anybase32.decode(RFC_4648, anybase32.RFC_4648) assert decoded == SAMPLE_TEXT
def test_rfc_2938(): decoded = anybase32.decode(RFC_2938, anybase32.RFC_2938) assert decoded == SAMPLE_TEXT
def test_zbase32(): decoded = anybase32.decode(ZBASE32, anybase32.ZBASE32) assert decoded == SAMPLE_TEXT
def test_rfc_2938(): decoded = anybase32.decode(RFC_2938, anybase32.RFC_2938) assert (decoded == SAMPLE_TEXT)
def test_repeat_character_alphabet(): with pytest.raises(ValueError) as e: anybase32.decode(RFC_3548, REPEAT_CHARACTER_ALPHABET) assert "Invalid base32 alphabet" in str(e)
def test_rfc_46488(): decoded = anybase32.decode(RFC_4648, anybase32.RFC_4648) assert (decoded == SAMPLE_TEXT)
def test_binary(): with open("tests/icon.png", "rb") as icon_file: icon_bytes = icon_file.read() decoded = anybase32.decode(ICON_ZBASE32, anybase32.ZBASE32) assert decoded == icon_bytes
def test_too_long_alphabet(): with pytest.raises(ValueError) as e: anybase32.decode(RFC_3548, TOO_LONG_ALPHABET) assert ("Invalid base32 alphabet" in str(e))
def test_wrong_alphabet(): with pytest.raises(TypeError) as e: anybase32.decode(RFC_3548, anybase32.NIGHTMARE) assert "Non-base32 digit found" in str(e)
def test_repeat_character_alphabet(): with pytest.raises(ValueError) as e: anybase32.decode(RFC_3548, REPEAT_CHARACTER_ALPHABET) assert ("Invalid base32 alphabet" in str(e))
import anybase32 data = "The **quick** brown fox jumps over the (lazy) dog!" print "Crockford modified", anybase32.encode(data, anybase32.CROCKFORD_MODIFIED) print "Crockford", anybase32.encode(data, anybase32.CROCKFORD) print "Zbase32", anybase32.encode(data, anybase32.ZBASE32) print "RFC 3548", anybase32.encode(data, anybase32.RFC_3548) print "RFC 2938", anybase32.encode(data, anybase32.RFC_2938) print "RFC 4648", anybase32.encode(data, anybase32.RFC_4648) print "Nightmare", anybase32.encode(data, anybase32.NIGHTMARE) enc = anybase32.encode(data, "0123456789`~!@#$%^&*()-_=+[]{}\/") print "Arbitrary", enc dec = anybase32.decode(enc, "0123456789`~!@#$%^&*()-_=+[]{}\/") print dec with open("tests/icon.png", "rb") as icon: icon_bytes = icon.read() icon = anybase32.encode(icon_bytes, anybase32.ZBASE32) while len(icon) > 74: print '"%s"' % icon[:74] icon = icon[74:]
def test_wrong_alphabet(): with pytest.raises(TypeError) as e: anybase32.decode(RFC_3548, anybase32.NIGHTMARE) assert ("Non-base32 digit found" in str(e))
def decode_base(self): encoded_base = self.encoded_base process_decode = self.process_decode # decoding as base16 try: process_decode( base64.b16decode(encoded_base, casefold=False).decode('utf-8', 'replace'), 'Base16') except Exception as _: pass # decoding as base32 try: process_decode( base64.b32decode(encoded_base, casefold=False, map01=None).decode('utf-8', 'replace'), 'Base32') self.b32_once = True except Exception as _: pass # decoding as base32 (RFC 3548) if not self.b32_once: try: """ Base32 charset can differ based on their spec, this requires stripping the padding or changing charsets to get the correct results. By default this `anybase32` implementation follows the RFC 3548 spec. """ temp_clean_base = str.encode(encoded_base.replace('=', '')) process_decode( anybase32.decode(temp_clean_base).decode( 'utf-8', 'replace'), 'Base32') except Exception as _: pass # decoding as base36 try: process_decode(base36.dumps(int(encoded_base)), 'Base36') except Exception as _: pass # decoding as base58 try: process_decode( base58.b58decode(encoded_base.encode()).decode( 'utf-8', 'replace'), 'Base58') except Exception as _: pass # decoding as base62 try: process_decode( base62.decodebytes(encoded_base).decode('utf-8', 'replace'), 'Base62') except Exception as _: pass # decoding as base64 try: process_decode( base64.b64decode(encoded_base).decode('utf-8', 'replace'), 'Base64') except Exception as _: pass # decoding as base64url try: process_decode( base64.urlsafe_b64decode(encoded_base + '=' * (4 - len(encoded_base) % 4)).decode( 'utf-8', 'replace'), 'Base64URL') except Exception as _: pass # decoding as base85 try: process_decode( base64.b85decode(encoded_base).decode('utf-8', 'replace'), 'Base85') except Exception as _: pass # decoding as ascii85 try: process_decode( base64.a85decode(encoded_base).decode('utf-8', 'replace'), 'Ascii85') except Exception as _: pass # decoding as base91 try: process_decode( base91.decode(encoded_base).decode('utf-8', 'replace'), 'Base91') except Exception as _: pass # decoding as base92 try: process_decode(base92.decode(encoded_base), 'Base92') except Exception as _: pass # decoding as base100 lol why??!! try: process_decode(pybase100.decode(encoded_base).decode(), 'Base100') except Exception as _: pass