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"))
相关推荐
OxYGC3 分钟前
[玩转GoLang] 5分钟整合Gin / Gorm框架入门
开发语言·golang·gin
锐策7 分钟前
Lua 核心知识点详解
开发语言·lua
kyle~24 分钟前
C/C++---动态内存管理(new delete)
c语言·开发语言·c++
萧鼎34 分钟前
深入解析 Python 的 pytun 库:虚拟网络接口与隧道技术实战指南
服务器·网络·python
西猫雷婶1 小时前
pytorch基本运算-分离计算
人工智能·pytorch·python·深度学习·神经网络·机器学习
数新网络1 小时前
PyTorch
人工智能·pytorch·python
自信的小螺丝钉1 小时前
【大模型手撕】pytorch实现LayerNorm, RMSNorm
人工智能·pytorch·python·归一化·rmsnorm·layernorm
深耕AI1 小时前
PyTorch图像预处理:ToTensor()与Normalize()的本质区别
人工智能·pytorch·python
落日沉溺于海1 小时前
React From表单使用Formik和yup进行校验
开发语言·前端·javascript
OpenBuild.xyz2 小时前
Web3 开发者周刊 66 | 重新审视L2:跨链桥与定序器背后的信任裂痕
web3·区块链·业界资讯·周报·数字货币