prs utility
Project description
prs-utility 是 PRS 为 Python 提供的算法工具库,包含项目中用到的消息摘要、签名算法。
Python 版本支持
支持 >= Python 3.6
安装
pip install prs-utility
使用示例
# 根据 keystore 和 password 得到私钥
import json
import prs_utility
keystore = {
"address": "758ea2601697fbd3ba6eb6774ed70b6c4cdb0ef9",
"crypto": {
"cipher": "aes-128-ctr",
"ciphertext": "92af6f6710eba271eae5ac7fec72c70d9f49215e7880a0c45d4c53e56bd7ea59",
"cipherparams": {
"iv": "13ddf95d970e924c97e4dcd29ba96520"
},
"mac": "b9d81d78f067334ee922fb2863e32c14cbc46e479eeb0acc11fb31e39256004e",
"kdf": "pbkdf2",
"kdfparams": {
"c": 262144,
"dklen": 32,
"prf": "hmac-sha256",
"salt": "79f90bb603491573e40a79fe356b88d0c7869852e43c2bbaabed44578a82bbfa"
}
},
"id": "93028e51-a2a4-4514-bc1a-94b089445f35",
"version": 3
}
password = '123123'
private_key = prs_utility.recover_private_key(
json.dumps(keystore), password
)
print('private_key:', private_key)
# 计算 hash 值
## 计算 bytes、int、bool 的 hash 值
print(prs_utility.keccak256(primitive=b'hello'))
print(prs_utility.keccak256(primitive=42))
print(prs_utility.keccak256(primitive=True))
注: 函数的第一个可选参数是 primitive,所以,可以不写该参数
print(prs_utility.keccak256(42))
## 计算 text 或 字符串 的 hash 值
print(prs_utility.keccak256(text='hello'))
with open(__file__) as fp:
content = fp.read()
file_hash = prs_utility.keccak256(text=content)
print('file hash:', file_hash)
## 计算 hex str 的 hash 值
print(prs_utility.keccak256(hexstr='0xabcd'))
# 根据 PRS 协议组合 block data, 并且使用 privateKey 进行签名
data = {
'file_hash': file_hash,
}
key_pair = prs_utility.create_key_pair()
private_key = key_pair['privateKey']
sig = prs_utility.sign_block_data(data, private_key)
print('signature:', sig)
# 生成一对新密钥
key_pair = prs_utility.create_key_pair()
print('key_pair:', key_pair)
API
prs-utility
提供了常用的加解密函数和一些用于格式转化的工具函数
$ pydoc prs_utility
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
prs-utility-0.0.8.tar.gz
(11.7 kB
view details)
Built Distribution
File details
Details for the file prs-utility-0.0.8.tar.gz
.
File metadata
- Download URL: prs-utility-0.0.8.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ba23426171be0a7e6667cc0fcab6979a0fe498ea75bc36b3a5e86f7b599fdd8 |
|
MD5 | 1ba409dcc90917a162db0c80ccef35b9 |
|
BLAKE2b-256 | 37049efa4a99230a8117ffa6436893189ab2454b4f447b15801986a1f6c18ff1 |
File details
Details for the file prs_utility-0.0.8-py2.py3-none-any.whl
.
File metadata
- Download URL: prs_utility-0.0.8-py2.py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.8.0 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2329954f8c895ce88dc35e2e95f9df5283b10ab0650c1e15dffb5025c47cda6 |
|
MD5 | adf01ecdf3e6979ed0288780baac1361 |
|
BLAKE2b-256 | d3138600f436f0723a3f36b02c34d8a144eccfb548da0c1818c0f4194f4c4a37 |