三联蟒的实现
TripleSec的Python项目详细描述
TripleSec库的python端口。另请参见jsimplementation。
与Python2.7和3.6+兼容。
安装
pip install TripleSec
用法
实例化一个triplesec.TripleSec(key=None)对象,有或没有键(如果省略,则每次使用时都必须指定),然后使用encrypt(message, key=None)和decrypt(ciphertext, key=None)方法。
所有值都必须是二进制字符串(python 2上的str,python 3上的bytes)
快捷方式
(未知的)函数encrypt和decrypt在模块级公开。
命令行工具
triplesec提供了一个triplesec命令行工具来加密和解密来自终端的消息。
以下是帮助:
Command-line TripleSec encryption-decryption tool usage: triplesec [-h] [-b | --hex] [-k KEY] {enc|dec} [data] positional arguments: {enc|dec} enc: encrypt and sign a message with TripleSec; by default output a hex encoded ciphertext (see -b and --hex) -- dec: decrypt and verify a TripleSec ciphertext data the TripleSec message or ciphertext; if not specified it will be read from stdin; by default ciphertexts will be considered hex encoded (see -b and --hex) optional arguments: -h, --help show this help message and exit -b, --binary consider all input (key, plaintext, ciphertext) to be plain binary data and output everything as binary data - this turns off smart decoding/encoding - if you pipe data, you should use this --hex consider all input (key, plaintext, ciphertext) to be hex encoded; hex encode all output -k KEY, --key KEY the TripleSec key; if not specified will check the TRIPLESEC_KEY env variable, then prompt the user for it --compatibility Use Keccak instead of SHA3 for the second MAC and reverse endianness of Salsa20 in version 1. Only effective in versions before 4.
0.5的变化
对于消息身份验证,triplesec规范在版本1到3中使用keccak sha3建议函数,但是在一段时间内,这个库使用了标准化的sha3-512函数。因此,默认情况下,版本1到3的python实现与javascript和golang实现不兼容。 从版本4开始,规范将只使用标准化的sha3-512函数(例如,在python中通过hashlib提供),python、javascript和golang实现将是兼容的。
如果您想在版本1到3中使用keccak(从而实现与node和go包的兼容性),您可以将compatibility=true传递到encrypt和decrypt,或者在命令行中传递,如上面一节中所述。
此外,未指定版本的加密现在将默认使用版本4,这与以前的版本不兼容。
示例
>>> import triplesec >>> x = triplesec.encrypt(b"IT'S A YELLOW SUBMARINE", b'* password *') >>> print(triplesec.decrypt(x, b'* password *').decode()) IT'S A YELLOW SUBMARINE
>>> from triplesec import TripleSec >>> T = TripleSec(b'* password *') >>> x = T.encrypt(b"IT'S A YELLOW SUBMARINE") >>> print(T.decrypt(x).decode()) IT'S A YELLOW SUBMARINE