python区块链简单模拟【01】

完整代码

https://gitee.com/ihan1001

https://github.com/ihan1001

重点:时间戳,MD5哈希,SHA256哈希,base64一种用64个字符表示任意二进制数据的方法,ECC椭圆曲线算法

bash 复制代码
import time
time.time()
bash 复制代码
datetime.now().strftime("%Y-%m-%d %H:%M:%S")
bash 复制代码
import hashlib
m=hashlib.md5()
m.update('使用md5加密的数据'.encode('utf-8'))
print(m.hexdigest())
bash 复制代码
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())
bash 复制代码
#base64 一种用64个字符表示任意二进制数据的方法
import base64
data = '你好,ihan'
#加密
result = base64.b64encode(data.encode('utf-8'))
print(result)
bash 复制代码
#解码
text = base64.b64decode(result)
print(text.decode('utf-8'))
bash 复制代码
pip install ecdsa
bash 复制代码
from ecdsa import SigningKey,SECP256k1    #椭圆曲线算法
#生成一对私钥和公钥
#私钥对字符串签名,公钥验证
#生成私钥
sk = SigningKey.generate(curve = SECP256k1)
sk
bash 复制代码
#生成公钥
vk = sk.get_verifying_key()
vk
bash 复制代码
#生成签名
signature = sk.sign("ihan".encode("utf-8"))
bash 复制代码
#验证签名
vk.verify(signature,"ihan".encode("utf-8"))
相关推荐
Warson_L11 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅11 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅11 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L11 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅12 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L12 小时前
python的类&继承
python
Warson_L13 小时前
类型标注/type annotation
python
ThreeS15 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵16 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏