簡體   English   中英

將PEM格式的openssl Ed25519私鑰加載到Python ed25519中。

[英]Load openssl Ed25519 private key in PEM format into Python ed25519.SigningKey

我有一些用openssl生成的密鑰:

openssl genpkey -algorithm Ed25519 -out private_key.pem

我想用它們在Python中生成ed25519簽名。 我找到了ed25519模塊,但是找不到將如上所述生成的PEM文件加載到ed25519.SigningKey

我該怎么做?

https://pypi.org/project/ed25519/建議改用https://github.com/pyca/pynacl

參考: https//pypi.org/project/ed25519/

不建議用於新應用程序:

使用pynacl代替對於新應用程序,我建議您使用[pynacl( https://github.com/pyca/pynacl )代替此存儲庫。 PyNaCl更大,並且構建時間更長(它包含完整的NaCl / libsodium庫,而不僅僅是ed25519部分),但是它由勤奮而認真的PyCA團隊很好地維護,而我卻允許該存儲庫陷入困境。 PyNaCl的速度也快約10-20倍。

要使用ed25519創建簽名,請參見https://pynacl.readthedocs.io/en/stable/signing/#example

簽名者的觀點(SigningKey)

import nacl.encoding
import nacl.signing

# Generate a new random signing key
signing_key = nacl.signing.SigningKey.generate()

# Sign a message with the signing key
signed = signing_key.sign(b"Attack at Dawn")

# Obtain the verify key for a given signing key
verify_key = signing_key.verify_key

# Serialize the verify key to send it to a third party
verify_key_hex = verify_key.encode(encoder=nacl.encoding.HexEncoder)

驗證者的觀點(VerifyKey)

import nacl.signing

# Create a VerifyKey object from a hex serialized public key
verify_key = nacl.signing.VerifyKey(verify_key_hex,
                                    encoder=nacl.encoding.HexEncoder)

# Check the validity of a message's signature
# The message and the signature can either be passed separately or
# concatenated together.  These are equivalent:
verify_key.verify(signed)
verify_key.verify(signed.message, signed.signature)

# Alter the signed message text
forged = signed[:-1] + bytes([int(signed[-1]) ^ 1])
# Will raise nacl.exceptions.BadSignatureError, since the signature check
# is failing
verify_key.verify(forged)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM