fix: support cryptography < 3.0 API for X25519 key serialization

private_bytes_raw() was added later; fall back to private_bytes(Raw)
for older system packages (e.g. Debian Bookworm python3-cryptography).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-22 09:33:23 -04:00
parent 42d27c85ef
commit a1a6b65e48
+6
View File
@@ -100,9 +100,15 @@ def _gen_keys_python():
"""Generate WireGuard keypair using the cryptography library (no wg binary needed).""" """Generate WireGuard keypair using the cryptography library (no wg binary needed)."""
import base64 import base64
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat, PrivateFormat, NoEncryption
private_key = X25519PrivateKey.generate() private_key = X25519PrivateKey.generate()
try:
# private_bytes_raw() available in cryptography >= 3.0
private_bytes = private_key.private_bytes_raw() private_bytes = private_key.private_bytes_raw()
public_bytes = private_key.public_key().public_bytes_raw() public_bytes = private_key.public_key().public_bytes_raw()
except AttributeError:
private_bytes = private_key.private_bytes(Encoding.Raw, PrivateFormat.Raw, NoEncryption())
public_bytes = private_key.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw)
return base64.b64encode(private_bytes).decode(), base64.b64encode(public_bytes).decode() return base64.b64encode(private_bytes).decode(), base64.b64encode(public_bytes).decode()