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"))
相关推荐
蹦蹦跳跳真可爱58910 分钟前
Python----数据分析(Matplotlib四:Figure的用法,创建Figure对象,常用的Figure对象的方法)
python·数据分析·matplotlib
小杨40440 分钟前
python入门系列六(文件操作)
人工智能·python·pycharm
今天也想MK代码1 小时前
rust编程实战:实现3d粒子渲染wasm
开发语言·rust·wasm
结衣结衣.1 小时前
【Qt】自定义信号和槽函数
开发语言·c++·qt·c++11
xiaozaq1 小时前
在Eclipse中安装Lombok插件
java·python·eclipse
云空1 小时前
《FastRTC:开启实时音视频应用开发新时代》
python·实时音视频
尘鹄2 小时前
一文讲懂Go语言如何使用配置文件连接数据库
开发语言·数据库·后端·golang
九丶黎2 小时前
爬虫案例七Python协程爬取视频
爬虫·python·音视频
qq_433554542 小时前
C++ 二叉搜索树代码
开发语言·c++·算法
benben0442 小时前
Django小白级开发入门
后端·python·django