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"))
相关推荐
databook5 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室6 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三7 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271111 小时前
Python之语言特点
python
刘立军11 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机14 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机16 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i17 小时前
django中的FBV 和 CBV
python·django
c8i17 小时前
python中的闭包和装饰器
python
这里有鱼汤20 小时前
小白必看:QMT里的miniQMT入门教程
后端·python