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"))
相关推荐
沈浩(种子思维作者)10 分钟前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算
2301_7903009615 分钟前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
weixin_4997715516 分钟前
C++中的组合模式
开发语言·c++·算法
初级代码游戏17 分钟前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
_waylau20 分钟前
鸿蒙架构师修炼之道-架构师的职责是什么?
开发语言·华为·harmonyos·鸿蒙
2的n次方_31 分钟前
CANN Ascend C 编程语言深度解析:异构并行架构、显式存储层级与指令级精细化控制机制
c语言·开发语言·架构
m0_7369191032 分钟前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
getapi34 分钟前
实时音视频传输与屏幕共享(投屏)
python
java干货1 小时前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法
_F_y1 小时前
C语言重点知识总结(含KMP详细讲解)
c语言·开发语言