python - 如何在 Python 中使用 Nettle?

标签 python nettle

Synaptic 包管理器中显示的包描述讲述了荨麻:

Nettle is a cryptographic library that is designed to fit easily in more or less any context: In crypto toolkits for object-oriented languages (C++, Python, Pike, ...), in applications like LSH or GNUPG, or even in kernel space.

但我无法在 Internet 上找到它的 Python 包装器。如何从 Python 控制它?

最佳答案

感谢 Vandan Revanur 的评论和 github 链接 python-nettle你可以在哪里download带有模块的 .zip 存档。

模块的安装可以按照 README.md 文件中的描述完成,并且需要运行两个 python 脚本。下面是 IDLE session 的摘录,显示了模块附带的内容:

Python 3.11.2 (main, Feb 11 2023, 10:33:12) [GCC 9.5.0] on linux
import nettle
dir(nettle)
['ASN1Error', 'BaseException', 'CBC', 'CCM', 'CTR', 'DataLenError', 'EAX', 'GCM', 'KeyLenError', 'LenMismatch', 'NotInitializedError', 'RSAError', 'RSAKeyPair', 'RSAPubKey', 'RandomError', 'Yarrow', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'aes128', 'aes192', 'aes256', 'arcfour', 'arctwo', 'autogen', 'base64', 'blowfish', 'camellia128', 'camellia192', 'camellia256', 'cast128', 'chacha', 'ciphers', 'des', 'des3', 'gosthash94', 'hashes', 'hmac_sha1', 'hmac_sha256', 'io', 'md2', 'md4', 'md5', 'poly1305_aes', 'pubkey', 're', 'ripemd160', 'salsa20', 'serpent', 'sha1', 'sha224', 'sha256', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'sha512_224', 'sha512_256', 'twofish', 'umac128', 'umac32', 'umac64', 'umac96']
nettle.md2.__doc__
'MD2 is another hash function of Ronald Rivest’s, described in RFC 1319. It outputs message digests of 128 bits, or 16 octets.'
nettle.md5.__doc__
'MD5 is a message digest function constructed by Ronald Rivest, and described in RFC 1321. It outputs message digests of 128 bits, or 16 octets.'
nettle.blowfish.__doc__
'BLOWFISH is a block cipher designed by Bruce Schneier. It uses a block size of 64 bits (8 octets), and a variable key size, up to 448 bits. It has some weak keys. '
nettle.sha1.__doc__
'SHA1 is a hash function specified by NIST (The U.S. National Institute for Standards and Technology).'

该模块似乎没有附带文档,但很容易找到如何使用它。下面是创建 MD5 的示例:

import nettle
ntte_md5obj = nettle.md5(b"some Text")  # expects a BYTE type object
ntte_md5obj.hexdigest() # gives '5B0989DEE5DA04D42D588B5B39578F35'
ntte_md5obj.digest_size # gives 16
ntte_md5obj.digest()    # gives b'[\t\x89\xde\xe5\xda\x04\xd4-X\x8b[9W\x8f5'

2023 年 2 月 21 日更新:nettle URL在两者之间列出可用的 Python 包装器模块。

关于python - 如何在 Python 中使用 Nettle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75513010/

相关文章:

javascript - 道场/请求 PUT 方法不起作用

Python - 骰子模拟器

python - 为什么 pickle.dumps 会调用 __getattr__?

python - 将元组转换为字典

gcc - 如何修复对 `nettle_sha256_digest` 的 undefined reference ?

配置 GnuTLS - 未找到 libnettle 3.4.1 - OS X Lion

c - 荨麻双鱼 CBC

python - 将 Python 数组 ('B' ) 转换为数组 ('H' ) - 总是小端?