哈希链日志实现
项目描述
HashLog
原则
- 记录是不可变的
- 记录是有序的
- 必须能够检查任何
i
的hash(Xi)
hash(Xi)
可以发送给任何人作为证据hash(Xi) == hash(X, hash(Xi-1))
- 您不需要 X 来执行检查
安装
$ pip install hlog
使用
入门
# create a chain
c = Chain()
# send some messages
c.append(message="Alice gives 10.36 euros to Bob", amount=10.36, currency="EUR")
c.append(message="Bob gives 2 dollars to Alice", amount=2, currency="USD")
# you can loop through the chain records
used_currencies = set(r.fields["currency"] for r in c.records)
# call verify() to ensure records have not been modified
c.verify()
验证期间抛出异常
c.verify(raise_on_error=True)
Verify 将抛出 ModifiedRecordException
。它有 3 个有趣的属性
index
:验证失败的链中索引message
:一个用户友好的消息,指示错误record
:本身的Record
对象
转储
c = Chain()
c.append(message="hello")
c.append(message="wonderful")
c.append(message="world")
rows = c.dump()
rows
是一个 tuple
的 bytes
,每个项是链中的一个记录。
从转储中恢复
如果您想根据文件或数据库记录重建链,可以使用 Chain.from_dump()
c = Chain()
c.append(message="hello")
c.append(message="wonderful")
c.append(message="world")
d = c.dump()
c2 = Chain.from_dump(d)
c2.verify()
项目详情
关闭
hlog-1.0.1.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 931ad958218bf64c4bb8299f850df30e77485a912db595d8e838e42c1a507f26 |
|
MD5 | 8acfa4b3e0388bbf2bda93586f14ec29 |
|
BLAKE2b-256 | 8cfb4f4e2afd30b56f9e31e05cbbdcb1876a547bbda2ee6ec9ef08d381587546 |