print()结果没有输出,找不到错误
Print () result is not output, can't find error
这个脚本生成了一个散列,但是它在某个地方没有正确地在函数中写入一些东西。
from bitcoin import *
import os
import hashlib
import base58
while True:
priv = random_key()
pubkey = privtopub(priv)
compress_pubkey = False
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
key_hash = hash160(hex_str)
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
我检查了脚本是否有效。做了print (pubkey)
。结果显示 public 键,但我不需要获取 key_hash。不幸的是,当我做 print ("key_hash = \ t" + rip.hexdigest ())
结果没有执行!我不懂编程。帮助修复代码!
代码重排后:
from bitcoin import *
import os
import hashlib
import base58
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update(sha.digest())
print("key_hash = \t" + rip.hexdigest())
return rip.hexdigest() # .hexdigest() is hex ASCII
while True:
priv = random_key()
pubkey = privtopub(priv)
compress_pubkey = False
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
key_hash = hash160(hex_str)
输出:
key_hash = b0ac6f690633331af487f594dd3c42c6c67ce085
key_hash = de735b3046545f63c8cb2f7d44b7f24a8b769ad7
key_hash = 49b0ae2b541832797680b977ca9e374d1a621787
key_hash = ea3e1d9762331e791412e96b2a67f418cfd6ca2c
key_hash = e5ff3affd4ba7eb2bb343548f587bde9dbdace6b
key_hash = b1a952405516abe494e7e32610a3eaf85d7914f2
key_hash = a0050e1f18b2d0738c458e237a447bd8f2810fec
key_hash = 23eeca93355ba511bdf28c475b5e62d2da64546b
key_hash = cc5904bae39ee51b75097c95ad0307a21ecef1bc
... And So On
注意有一个无限循环(while True
),考虑用特定的迭代次数替换。
这个脚本生成了一个散列,但是它在某个地方没有正确地在函数中写入一些东西。
from bitcoin import *
import os
import hashlib
import base58
while True:
priv = random_key()
pubkey = privtopub(priv)
compress_pubkey = False
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
key_hash = hash160(hex_str)
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
我检查了脚本是否有效。做了print (pubkey)
。结果显示 public 键,但我不需要获取 key_hash。不幸的是,当我做 print ("key_hash = \ t" + rip.hexdigest ())
结果没有执行!我不懂编程。帮助修复代码!
代码重排后:
from bitcoin import *
import os
import hashlib
import base58
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update(sha.digest())
print("key_hash = \t" + rip.hexdigest())
return rip.hexdigest() # .hexdigest() is hex ASCII
while True:
priv = random_key()
pubkey = privtopub(priv)
compress_pubkey = False
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
key_hash = hash160(hex_str)
输出:
key_hash = b0ac6f690633331af487f594dd3c42c6c67ce085
key_hash = de735b3046545f63c8cb2f7d44b7f24a8b769ad7
key_hash = 49b0ae2b541832797680b977ca9e374d1a621787
key_hash = ea3e1d9762331e791412e96b2a67f418cfd6ca2c
key_hash = e5ff3affd4ba7eb2bb343548f587bde9dbdace6b
key_hash = b1a952405516abe494e7e32610a3eaf85d7914f2
key_hash = a0050e1f18b2d0738c458e237a447bd8f2810fec
key_hash = 23eeca93355ba511bdf28c475b5e62d2da64546b
key_hash = cc5904bae39ee51b75097c95ad0307a21ecef1bc
... And So On
注意有一个无限循环(while True
),考虑用特定的迭代次数替换。