Created
September 4, 2013 03:19
-
-
Save grimd34th/6432412 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string, sexuaddr | |
TOX_ADDR="5A528ABA260B66096AAD1355CA6918D41158A7B7D0134684DF55722D25194369D4D37F7B7B77" | |
if (all(c in string.hexdigits for c in TOX_ADDR)): | |
print sexuaddr.tox_to_sexu(TOX_ADDR) | |
else: | |
print "Invalid Tox Addr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##IMPORTING | |
import math | |
try: | |
import Crypto.Hash.SHA256 as SHA256 | |
import Crypto.Hash.RIPEMD160 as RIPEMD160 | |
have_crypto = True | |
except ImportError: | |
have_crypto = False | |
##BASE58 | |
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |
__b58base = len(__b58chars) | |
def b58encode(v): | |
long_value = 0L | |
for (i, c) in enumerate(v[::-1]): | |
long_value += ord(c) << (8*i) | |
result = '' | |
while long_value >= __b58base: | |
div, mod = divmod(long_value, __b58base) | |
result = __b58chars[mod] + result | |
long_value = div | |
result = __b58chars[long_value] + result | |
nPad = 0 | |
for c in v: | |
if c == '\0': nPad += 1 | |
else: break | |
return (__b58chars[0]*nPad) + result | |
def b58decode(v, length): | |
""" decode v into a string of len bytes | |
""" | |
long_value = 0L | |
for (i, c) in enumerate(v[::-1]): | |
long_value += __b58chars.find(c) * (__b58base**i) | |
result = '' | |
while long_value >= 256: | |
div, mod = divmod(long_value, 256) | |
result = chr(mod) + result | |
long_value = div | |
result = chr(long_value) + result | |
nPad = 0 | |
for c in v: | |
if c == __b58chars[0]: nPad += 1 | |
else: break | |
result = chr(0)*nPad + result | |
if length is not None and len(result) != length: | |
return None | |
return result | |
## CONVERTING ADDR TO SEXU FORMAT | |
def tox_to_sexu(addr): | |
a1="\x539"+RIPEMD160.new(SHA256.new(addr).digest()).digest() | |
a2=SHA256.new(SHA256.new(a1).digest()).digest() | |
taddr=a1+a2[0:4] | |
return b58encode(taddr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment