fork(30) download
  1. import ecdsa
  2. import ecdsa.der
  3. import ecdsa.util
  4. import hashlib
  5. import os
  6. import re
  7. import struct
  8.  
  9. b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  10.  
  11. def base58encode(n):
  12. result = ''
  13. while n > 0:
  14. result = b58[n%58] + result
  15. n /= 58
  16. return result
  17.  
  18. def base256decode(s):
  19. result = 0
  20. for c in s:
  21. result = result * 256 + ord(c)
  22. return result
  23.  
  24. def countLeadingChars(s, ch):
  25. count = 0
  26. for c in s:
  27. if c == ch:
  28. count += 1
  29. else:
  30. break
  31. return count
  32.  
  33. # https://e...content-available-to-author-only...n.it/wiki/Base58Check_encoding
  34. def base58CheckEncode(version, payload):
  35. s = chr(version) + payload
  36. checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
  37. result = s + checksum
  38. leadingZeros = countLeadingChars(result, '\0')
  39. return '1' * leadingZeros + base58encode(base256decode(result))
  40.  
  41. def privateKeyToWif(key_hex):
  42. return base58CheckEncode(0x80, key_hex.decode('hex'))
  43.  
  44. def privateKeyToPublicKey(s):
  45. sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1)
  46. vk = sk.verifying_key
  47. return ('\04' + sk.verifying_key.to_string()).encode('hex')
  48.  
  49. def pubKeyToAddr(s):
  50. ripemd160 = hashlib.new('ripemd160')
  51. ripemd160.update(hashlib.sha256(s.decode('hex')).digest())
  52. return base58CheckEncode(0, ripemd160.digest())
  53.  
  54. def keyToAddr(s):
  55. return pubKeyToAddr(privateKeyToPublicKey(s))
  56.  
  57. # Generate a random private key
  58. private_key = os.urandom(32).encode('hex')
  59.  
  60. # You can verify the values on http://b...content-available-to-author-only...t.org/
  61. print "Secret Exponent (Uncompressed) : %s " % private_key
  62. print "Private Key : %s " % privateKeyToWif(private_key)
  63. print "Address : %s " % keyToAddr(private_key)
Runtime error #stdin #stdout #stderr 0.02s 9016KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
ImportError: No module named ecdsa