hashlib - 如何从 .txt 列表中获取数据并将结果保存在另一个 .txt 中?
问题描述
我不知道 Python 语言,但我需要更改此代码:
#!/usr/bin/env python
# https://en.bitcoin.it/wiki/Protocol_documentation#Addresses
import hashlib
import base58
# ECDSA bitcoin Public Key
pubkey = '02e026fdf8f48819c1d6a3711cd547d82cc9de5d5fed05316cdaeddddd503cf233'
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures
compress_pubkey = False
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
if (compress_pubkey):
if (ord(bytearray.fromhex(pubkey[-2:])) % 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)
# Obtain key:
key_hash = '00' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( bytearray.fromhex(key_hash) )
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]
print ( "" + (base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum)) )).decode('utf-8') )
这里需要从文件列表中取出公钥数据:Pubkey.txt 并将处理后的数据保存到新文件:Results.txt结果存储在另一个文本文档中。
解决方案
您无需导入库即可在 python 中读取和写入文件:
#reading
file = open("Pubkey.txt","r")
file.read()
file.close()
#writing
file = open("Results.txt","w")
file.write(data)
file.close()
推荐阅读
- mysql - 获取mysql表中两行中两个数字的差
- php - How to exit script if external call exceeds time limit
- emscripten - Is there a reasonable way to create a webassembly "static library" in a multi-module build for emscripten
- dns - Wildcard DNS resolution failed when adding TXT record
- java - 运行时没有 onMessage 事件(Oracle 队列上的 Java JMS MessageListener)
- mysql - Sum, Group Concat and Join with 3 tables.
- reactjs - react js中getDerivedStateFromProps中的多个道具验证
- c# - Form window not opening when I run the program because of a line in c#
- azure-web-app-service - 使用 javascript 在 html Web 应用程序中的 Azure 时间序列见解可视化
- opencart - opencart display_errors on, error_reporting = E_ALL