1 生成过程
比特币地址生成流程如下图所示:
第一步,随机选取一个32字节的数,大小介于1~0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4141之间,作为私钥
18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725
第二步,使用椭圆曲线加密算法(ECDSA-SECP256k1)计算私钥所对应的非压缩公钥(共65字节,1字节0x04,32字节为x坐标,32字节为y坐标)。
0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
第三步,计算公钥的SHA-256哈希值
600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408
第四步,计算上一步哈希值的RIPEMD-160哈希值
010966776006953D5567439E5E39F86A0D273BEE
第五步,在上一步结果之间加入地址版本号(如比特币主网版本号"0x00")
00010966776006953D5567439E5E39F86A0D273BEE
第六步,计算上一步结果的SHA-256哈希值
445C7A8007A93D8733188288BB320A8FE2DEBD2AE1B47F0F50BC10BAE845C094
第七步,再次计算上一步结果的SHA-256哈希值
D61967F63C7DD183914A4AE452C9F6AD5D462CE3D277798075B107615C1A8A30
第八步,取上一步结果的前4个字节(8位十六进制数)D61967F6,把这4个字节加在第五步结果的后面,作为校验(这就是比特币地址的16进制形态)
00010966776006953D5567439E5E39F86A0D273BEED61967F6
第九步,用base58表示法变换一下地址(这就是最常见的比特币地址形态)
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
下面给出比特币地址生成的python源码
1 import hashlib 2 from ecdsa import SECP256k1, SigningKey 3 import sys 4 import binascii 5 6 # 58 character alphabet used 7 BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' 8 9 def from_bytes (data, big_endian = False): 10 if isinstance(data, str): 11 data = bytearray(data) 12 if big_endian: 13 data = reversed(data) 14 num = 0 15 for offset, byte in enumerate(data): 16 num += byte << (offset * 8) 17 return num 18 19 def base58_encode(version, public_address): 20 """ 21 Gets a Base58Check string 22 See https://en.bitcoin.it/wiki/Base58Check_encoding 23 """ 24 if sys.version_info.major > 2: 25 version = bytes.fromhex(version) 26 else: 27 version = bytearray.fromhex(version) 28 firstSHA256 = hashlib.sha256(version + public_address) 29 print("first sha256: %s"%firstSHA256.hexdigest().upper()) 30 secondSHA256 = hashlib.sha256(firstSHA256.digest()) 31 print("second sha256: %s"%secondSHA256.hexdigest().upper()) 32 checksum = secondSHA256.digest()[:4] 33 payload = version + public_address + checksum 34 print("Hex address: %s"%binascii.hexlify(payload).decode().upper()) 35 if sys.version_info.major > 2: 36 result = int.from_bytes(payload, byteorder="big") 37 else: 38 result = from_bytes(payload, True) 39 # count the leading 0s 40 padding = len(payload) - len(payload.lstrip(b'