Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

base58.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. '''
  2. Bitcoin base58 encoding and decoding.
  3. Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain)
  4. '''
  5. import hashlib
  6. # for compatibility with following code...
  7. class SHA256:
  8. new = hashlib.sha256
  9. if str != bytes:
  10. # Python 3.x
  11. def ord(c):
  12. return c
  13. def chr(n):
  14. return bytes( (n,) )
  15. __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  16. __b58base = len(__b58chars)
  17. b58chars = __b58chars
  18. def b58encode(v):
  19. """ encode v, which is a string of bytes, to base58.
  20. """
  21. long_value = 0
  22. for (i, c) in enumerate(v[::-1]):
  23. long_value += (256**i) * ord(c)
  24. result = ''
  25. while long_value >= __b58base:
  26. div, mod = divmod(long_value, __b58base)
  27. result = __b58chars[mod] + result
  28. long_value = div
  29. result = __b58chars[long_value] + result
  30. # Bitcoin does a little leading-zero-compression:
  31. # leading 0-bytes in the input become leading-1s
  32. nPad = 0
  33. for c in v:
  34. if c == '\0': nPad += 1
  35. else: break
  36. return (__b58chars[0]*nPad) + result
  37. def b58decode(v, length = None):
  38. """ decode v into a string of len bytes
  39. """
  40. long_value = 0
  41. for (i, c) in enumerate(v[::-1]):
  42. long_value += __b58chars.find(c) * (__b58base**i)
  43. result = bytes()
  44. while long_value >= 256:
  45. div, mod = divmod(long_value, 256)
  46. result = chr(mod) + result
  47. long_value = div
  48. result = chr(long_value) + result
  49. nPad = 0
  50. for c in v:
  51. if c == __b58chars[0]: nPad += 1
  52. else: break
  53. result = chr(0)*nPad + result
  54. if length is not None and len(result) != length:
  55. return None
  56. return result
  57. def checksum(v):
  58. """Return 32-bit checksum based on SHA256"""
  59. return SHA256.new(SHA256.new(v).digest()).digest()[0:4]
  60. def b58encode_chk(v):
  61. """b58encode a string, with 32-bit checksum"""
  62. return b58encode(v + checksum(v))
  63. def b58decode_chk(v):
  64. """decode a base58 string, check and remove checksum"""
  65. result = b58decode(v)
  66. if result is None:
  67. return None
  68. h3 = checksum(result[:-4])
  69. if result[-4:] == checksum(result[:-4]):
  70. return result[:-4]
  71. else:
  72. return None
  73. def get_bcaddress_version(strAddress):
  74. """ Returns None if strAddress is invalid. Otherwise returns integer version of address. """
  75. addr = b58decode_chk(strAddress)
  76. if addr is None or len(addr)!=21: return None
  77. version = addr[0]
  78. return ord(version)
  79. if __name__ == '__main__':
  80. # Test case (from http://gitorious.org/bitcoin/python-base58.git)
  81. assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') is 0
  82. _ohai = 'o hai'.encode('ascii')
  83. _tmp = b58encode(_ohai)
  84. assert _tmp == 'DYB3oMS'
  85. assert b58decode(_tmp, 5) == _ohai
  86. print("Tests passed")