Guest User

Untitled

a guest
Nov 20th, 2013
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Read a private key from stdin and output formatted data values.
  4. # Input one key per line either hex (64 chars) or WIF key (51 base 58 chars).
  5. #
  6. # The format argument can contain variables:
  7. #
  8. # %h = HEX privkey
  9. # %w = WIF privkey
  10. # %p = public key
  11. # %a = address
  12. #
  13. # eg. "Address: %a\nPrivkey: %w" outputs a format like the vanitygen program
  14. #     "a:%w" outputs a format good for importing to Electrum
  15. #
  16. # This generates a new key for importing to Electrum:
  17. #
  18. #     hexdump -v -e '/1 "%02X"' -n 32 /dev/urandom | keyfmt "%a:%w"
  19. #
  20.  
  21. import sys, binascii, hashlib
  22.  
  23. if len(sys.argv) < 2:
  24.     print "Usage: %s <format string>\n\nRead a hex private key from stdin and output formatted data.\n" % sys.argv[0]
  25.     print "These variables are supported:\n%h = HEX privkey\n%w = WIF privkey\n%p = public key\n%a = address\n"
  26.     print "eg. 'Address: %a\\nPrivkey: %w' outputs a format like the vanitygen program\n"
  27.     sys.exit(1)
  28.  
  29. alphabet="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  30. advanced = any(v in sys.argv[1] for v in ['%p', '%a'])
  31.  
  32. def b58encode(num, pad=''):
  33.     out = ''
  34.     while num >= 58:
  35.         num,m = divmod(num, 58)
  36.         out = alphabet[m] + out
  37.     return pad + alphabet[num] + out
  38.  
  39. def b58hex(s58):
  40.     num = 0L
  41.     for (i, c) in enumerate(s58[::-1]):
  42.         num += alphabet.find(c) * (58**i)
  43.     return hex(num)[4:-9].upper()
  44.    
  45. if advanced:
  46.     import ecdsa
  47.  
  48.     # secp256k1, http://www.oid-info.com/get/1.3.132.0.10
  49.     _p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
  50.     _r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
  51.     _b = 0x0000000000000000000000000000000000000000000000000000000000000007L
  52.     _a = 0x0000000000000000000000000000000000000000000000000000000000000000L
  53.     _Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
  54.     _Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
  55.     curve_secp256k1 = ecdsa.ellipticcurve.CurveFp( _p, _a, _b )
  56.     generator_secp256k1 = ecdsa.ellipticcurve.Point( curve_secp256k1, _Gx, _Gy, _r )
  57.     oid_secp256k1 = (1,3,132,0,10)
  58.     SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1 )
  59.  
  60. for line in sys.stdin:
  61.     line = line.replace(' ', '')
  62.     line = b58hex(line[:51]) if line[0] == '5' and len(line) < 64 else line[:64]
  63.     chksum = binascii.hexlify(hashlib.sha256(hashlib.sha256(binascii.unhexlify('80'+line)).digest()).digest()[:4])
  64.     privkey = long('80'+line+chksum, 16)
  65.    
  66.     if advanced:
  67.         pubkey = chr(4) + ecdsa.SigningKey.from_secret_exponent(long(line, 16), curve=SECP256k1 ).get_verifying_key().to_string()
  68.         pad = ""
  69.         rmd = hashlib.new('ripemd160')
  70.         rmd.update(hashlib.sha256(pubkey).digest())
  71.         an = chr(0) + rmd.digest()
  72.         for c in an:
  73.             if c == '\0': pad += '1'
  74.             else: break
  75.         addr = long(binascii.hexlify(an + hashlib.sha256(hashlib.sha256(an).digest()).digest()[0:4]), 16)
  76.    
  77.     out = sys.argv[1].replace('%h', line).replace('%w', b58encode(privkey))
  78.     if advanced:
  79.         out = out.replace('%p', binascii.hexlify(pubkey).upper()).replace('%a', b58encode(addr, pad))
  80.     print out.decode('string-escape')
Advertisement
Add Comment
Please, Sign In to add comment