Simple simulation of python blockchain【01】

Complete code
https://gitee.com/ihan1001
https://github.com/ihan1001
Key points: timestamp, MD5 hash, SHA256 hash, base64, a method of representing arbitrary binary data with 64 characters, ECC elliptic curve algorithm

import time
time.time()

Insert image description here

datetime.now().strftime("%Y-%m-%d %H:%M:%S")

Insert image description here

import hashlib
m=hashlib.md5()
m.update('使用md5加密的数据'.encode('utf-8'))
print(m.hexdigest())

Insert image description here

s = hashlib.sha256()
h = hashlib.sha256()
s.update('i'.encode('utf-8'))
s.update('h'.encode('utf-8'))
s.update('a'.encode('utf-8'))
s.update('h'.encode('utf-8'))
print(s.hexdigest())
h.update('ihan'.encode('utf-8'))
print(h.hexdigest())

Insert image description here

#base64 一种用64个字符表示任意二进制数据的方法
import base64
data = '你好,ihan'
#加密
result = base64.b64encode(data.encode('utf-8'))
print(result)

Insert image description here

#解码
text = base64.b64decode(result)
print(text.decode('utf-8'))

Insert image description here

pip install ecdsa

Insert image description here

from ecdsa import SigningKey,SECP256k1    #椭圆曲线算法
#生成一对私钥和公钥
#私钥对字符串签名,公钥验证
#生成私钥
sk = SigningKey.generate(curve = SECP256k1)
sk

Insert image description here

#生成公钥
vk = sk.get_verifying_key()
vk
#生成签名
signature = sk.sign("ihan".encode("utf-8"))
#验证签名
vk.verify(signature,"ihan".encode("utf-8"))

Guess you like

Origin blog.csdn.net/weixin_43491496/article/details/135160539