pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Sleepyhead on Mon 9 Dec 15:08
report abuse | download | new post

  1. # Bitcoin address generation code by weex & Joric
  2. # Original bitcoin address generation code can be found here:
  3. # https://github.com/weex/addrgen
  4. #
  5. #
  6. # Repurposed for use as a vanity address generator by Sleepyhead
  7. # (Seriously though, the modifications I did were simple as fuck. Mad props to weex
  8. # and Joric, they did all the heavy lifting here, not me.)
  9. #
  10. #
  11. # Notice: To cut down on filesize and whatnot, I've removed lots of comments and white
  12. # space that weex & Joric had included in the original file.
  13. #
  14. #
  15. # Usage: btcvanity.py words
  16. # The above will generate a bitcoin that starts with 1words
  17. # (In case you're unaware, all bitcoin addresses start with 1)
  18.  
  19. import hashlib
  20. import ctypes
  21. import ctypes.util
  22. import sys
  23. import thread
  24.  
  25. ssl = ctypes.cdll.LoadLibrary (ctypes.util.find_library ('ssl') or 'libeay32')
  26.  
  27. def check_result (val, func, args):
  28.         if val == 0: raise ValueError
  29.         else: return ctypes.c_void_p (val)
  30.  
  31. ssl.EC_KEY_new_by_curve_name.restype = ctypes.c_void_p
  32. ssl.EC_KEY_new_by_curve_name.errcheck = check_result
  33.  
  34. class KEY:
  35.         def __init__(self):
  36.                 NID_secp256k1 = 714
  37.                 self.k = ssl.EC_KEY_new_by_curve_name(NID_secp256k1)
  38.                 self.compressed = False
  39.                 self.POINT_CONVERSION_COMPRESSED = 2
  40.                 self.POINT_CONVERSION_UNCOMPRESSED = 4
  41.         def __del__(self):
  42.                 if ssl:
  43.                         ssl.EC_KEY_free(self.k)
  44.                 self.k = None
  45.         def generate(self, secret=None):
  46.                 if secret:
  47.                         self.prikey = secret
  48.                         priv_key = ssl.BN_bin2bn(secret, 32, ssl.BN_new())
  49.                         group = ssl.EC_KEY_get0_group(self.k)
  50.                         pub_key = ssl.EC_POINT_new(group)
  51.                         ctx = ssl.BN_CTX_new()
  52.                         ssl.EC_POINT_mul(group, pub_key, priv_key, None, None, ctx)
  53.                         ssl.EC_KEY_set_private_key(self.k, priv_key)
  54.                         ssl.EC_KEY_set_public_key(self.k, pub_key)
  55.                         ssl.EC_POINT_free(pub_key)
  56.                         ssl.BN_CTX_free(ctx)
  57.                         return self.k
  58.                 else:
  59.                         return ssl.EC_KEY_generate_key(self.k)
  60.         def get_pubkey(self):
  61.                 size = ssl.i2o_ECPublicKey(self.k, 0)
  62.                 mb = ctypes.create_string_buffer(size)
  63.                 ssl.i2o_ECPublicKey(self.k, ctypes.byref(ctypes.pointer(mb)))
  64.                 return mb.raw
  65.         def get_secret(self):
  66.                 bn = ssl.EC_KEY_get0_private_key(self.k);
  67.                 bytes = (ssl.BN_num_bits(bn) + 7) / 8
  68.                 mb = ctypes.create_string_buffer(bytes)
  69.                 n = ssl.BN_bn2bin(bn, mb);
  70.                 return mb.raw.rjust(32, chr(0))
  71.         def set_compressed(self, compressed):
  72.                 self.compressed = compressed
  73.                 if compressed:
  74.                         form = self.POINT_CONVERSION_COMPRESSED
  75.                 else:
  76.                         form = self.POINT_CONVERSION_UNCOMPRESSED
  77.                 ssl.EC_KEY_set_conv_form(self.k, form)
  78. def dhash(s):
  79.         return hashlib.sha256(hashlib.sha256(s).digest()).digest()
  80. def rhash(s):
  81.         h1 = hashlib.new('ripemd160')
  82.         h1.update(hashlib.sha256(s).digest())
  83.         return h1.digest()
  84. b58_digits = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  85. def base58_encode(n):
  86.         l = []
  87.         while n > 0:
  88.                 n, r = divmod(n, 58)
  89.                 l.insert(0,(b58_digits[r]))
  90.         return ''.join(l)
  91. def base58_decode(s):
  92.         n = 0
  93.         for ch in s:
  94.                 n *= 58
  95.                 digit = b58_digits.index(ch)
  96.                 n += digit
  97.         return n
  98. def base58_encode_padded(s):
  99.         res = base58_encode(int('0x' + s.encode('hex'), 16))
  100.         pad = 0
  101.         for c in s:
  102.                 if c == chr(0):
  103.                         pad += 1
  104.                 else:
  105.                         break
  106.         return b58_digits[0] * pad + res
  107. def base58_decode_padded(s):
  108.         pad = 0
  109.         for c in s:
  110.                 if c == b58_digits[0]:
  111.                         pad += 1
  112.                 else:
  113.                         break
  114.         h = '%x' % base58_decode(s)
  115.         if len(h) % 2:
  116.                 h = '0' + h
  117.         res = h.decode('hex')
  118.         return chr(0) * pad + res
  119. def base58_check_encode(s, version=0):
  120.         vs = chr(version) + s
  121.         check = dhash(vs)[:4]
  122.         return base58_encode_padded(vs + check)
  123. def base58_check_decode(s, version=0):
  124.         k = base58_decode_padded(s)
  125.         v0, data, check0 = k[0], k[1:-4], k[-4:]
  126.         check1 = dhash(v0 + data)[:4]
  127.         if check0 != check1:
  128.                 raise BaseException('checksum error')
  129.         if version != ord(v0):
  130.                 raise BaseException('version mismatch')
  131.         return data
  132. def gen_eckey(passphrase=None, secret=None, pkey=None, compressed=False, rounds=1, version=0):
  133.         k = KEY()
  134.         if passphrase:
  135.                 secret = passphrase.encode('utf8')
  136.                 for i in xrange(rounds):
  137.                         secret = hashlib.sha256(secret).digest()
  138.         if pkey:
  139.                 secret = base58_check_decode(pkey, 128+version)
  140.                 compressed = len(secret) == 33
  141.                 secret = secret[0:32]
  142.         k.generate(secret)
  143.         k.set_compressed(compressed)
  144.         return k
  145. def get_addr(k,version=0):
  146.         pubkey = k.get_pubkey()
  147.         secret = k.get_secret()
  148.         hash160 = rhash(pubkey)
  149.         addr = base58_check_encode(hash160,version)
  150.         payload = secret
  151.         if k.compressed:
  152.                 payload = secret + chr(1)
  153.         pkey = base58_check_encode(payload, 128+version)
  154.         return addr, pkey
  155. def reencode(pkey,version=0):
  156.         payload = base58_check_decode(pkey,128+version)
  157.         secret = payload[:-1]
  158.         payload = secret + chr(1)
  159.         pkey = base58_check_encode(payload, 128+version)
  160.         print get_addr(gen_eckey(pkey))
  161. def test(otherversion):
  162.         return get_addr(gen_eckey(compressed=True,version=otherversion),version=otherversion)
  163. def bruteforce():
  164.         import re
  165.         count = 0
  166.         while 1:
  167.                 count += 1
  168.                 newaddr = test(int(options.otherversion))
  169.                 privkey = newaddr[1]
  170.                 btcaddr = newaddr[0]
  171.                 if btcaddr.startswith("1" + sys.argv[1]):
  172. #               if sys.argv[1] in btcaddr:
  173.                         return "Private key: " + privkey + "\nBitcoin address: " + btcaddr + "\nFound in " + str(count) + " tries"
  174. if __name__ == '__main__':
  175.         import optparse
  176.         parser = optparse.OptionParser(usage="%prog [options]")
  177.         parser.add_option("--otherversion", dest="otherversion", default=0,
  178.                                         help="Generate address with different version number")
  179.         (options, args) = parser.parse_args()
  180.  
  181.         print bruteforce()
  182.         #print test(int(options.otherversion))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post