fork download
  1. import hashlib
  2. import sys
  3.  
  4. ############################################################################
  5. '''
  6. [This section is cloned code of Python module "base58"]
  7. [Compatible with "from base58 import *"]
  8.  
  9. Copyright (c) 2015 David Keijser
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17.  
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. THE SOFTWARE.
  28. '''
  29.  
  30. alphabet = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  31.  
  32. if bytes == str: # python2
  33. iseq, bseq, buffer = (
  34. lambda s: map(ord, s),
  35. lambda s: ''.join(map(chr, s)),
  36. lambda s: s,
  37. )
  38. else: # python3
  39. iseq, bseq, buffer = (
  40. lambda s: s,
  41. bytes,
  42. lambda s: s.buffer,
  43. )
  44.  
  45.  
  46. def scrub_input(v):
  47. if isinstance(v, str) and not isinstance(v, bytes):
  48. v = v.encode('ascii')
  49.  
  50. if not isinstance(v, bytes):
  51. raise TypeError(
  52. "a bytes-like object is required (also str), not '%s'" %
  53. type(v).__name__)
  54.  
  55. return v
  56.  
  57.  
  58. def b58encode_int(i, default_one=True):
  59. '''Encode an integer using Base58'''
  60. if not i and default_one:
  61. return alphabet[0:1]
  62. string = b""
  63. while i:
  64. i, idx = divmod(i, 58)
  65. string = alphabet[idx:idx+1] + string
  66. return string
  67.  
  68.  
  69. def b58encode(v):
  70. '''Encode a string using Base58'''
  71.  
  72. v = scrub_input(v)
  73.  
  74. nPad = len(v)
  75. v = v.lstrip(b'\0')
  76. nPad -= len(v)
  77.  
  78. p, acc = 1, 0
  79. for c in iseq(reversed(v)):
  80. acc += p * c
  81. p = p << 8
  82.  
  83. result = b58encode_int(acc, default_one=False)
  84.  
  85. return (alphabet[0:1] * nPad + result)
  86.  
  87.  
  88. def b58decode_int(v):
  89. '''Decode a Base58 encoded string as an integer'''
  90.  
  91. v = scrub_input(v)
  92.  
  93. decimal = 0
  94. for char in v:
  95. decimal = decimal * 58 + alphabet.index(char)
  96. return decimal
  97.  
  98.  
  99. def b58decode(v):
  100. '''Decode a Base58 encoded string'''
  101.  
  102. v = scrub_input(v)
  103.  
  104. origlen = len(v)
  105. v = v.lstrip(alphabet[0:1])
  106. newlen = len(v)
  107.  
  108. acc = b58decode_int(v)
  109.  
  110. result = []
  111. while acc > 0:
  112. acc, mod = divmod(acc, 256)
  113. result.append(mod)
  114.  
  115. return (b'\0' * (origlen - newlen) + bseq(reversed(result)))
  116. ############################################################################
  117.  
  118.  
  119. text = "1" + input()
  120.  
  121. try:
  122. b58decode(text)
  123. except Exception as e:
  124. print("Contains invalid base58 charactor!\nYou can use only " + alphabet.decode())
  125. exit()
  126.  
  127. if len(b58decode(text)) > 25:
  128. print("Too long text! Within 25.")
  129. exit()
  130.  
  131. while len(b58decode(text)) < 25:
  132. text += "X"
  133.  
  134. decoded = b58decode(text)
  135. chk = hashlib.sha256(hashlib.sha256(decoded[0:21]).digest()).digest()[0:4]
  136. checked_decoded = decoded[0:21] + chk
  137. print(b58encode(checked_decoded).decode())
Success #stdin #stdout 0.04s 11420KB
stdin
MankoChinkoUnko
stdout
1MankoChinkoUnkoXXXXXXXXXXXU9Fmsi