fork download
  1. from hashlib import sha256
  2.  
  3. digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  4.  
  5. def decode_base58(bc, length):
  6. n = 0
  7. for char in bc:
  8. n = n * 58 + digits58.index(char)
  9. return n.to_bytes(length, 'big')
  10.  
  11. def check_bc(bc):
  12. bcbytes = decode_base58(bc, 25)
  13. return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
  14.  
  15. if __name__ == '__main__':
  16. bc = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
  17. assert check_bc(bc)
  18. assert not check_bc( bc.replace('N', 'P', 1) )
  19. assert check_bc('1111111111111111111114oLvT2')
  20. assert check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j")
Success #stdin #stdout 0.03s 11108KB
stdin
from hashlib import sha256

digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def decode_base58(bc, length):
 n = 0
 for char in bc:
  n = n * 58 + digits58.index(char)
 return n.to_bytes(length, 'big')
 
def check_bc(bc):
 bcbytes = decode_base58(bc, 25)
 return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]

if __name__ == '__main__':
 bc = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
 assert check_bc(bc)
 assert not check_bc( bc.replace('N', 'P', 1) )
 assert check_bc('1111111111111111111114oLvT2')
 assert check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j")
stdout
Standard output is empty