x25519
The `x25519` library (version 0.0.2) provides a pure Python implementation of the Curve25519 elliptic curve for Diffie-Hellman key exchange. It was last released in October 2021 and appears to be unmaintained, with no active development or official GitHub repository discoverable at the provided link. This library is distinct from the more robust and actively maintained X25519 implementations found in the `cryptography` library.
Common errors
-
AttributeError: module 'x25519' has no attribute 'X25519PrivateKey'
cause Attempting to use `cryptography` library's object-oriented API with the `x25519` pure Python package.fixThe `x25519` package uses functional calls (e.g., `x25519.scalar_base_mult`). If you intended to use the `cryptography` library, ensure it's installed (`pip install cryptography`) and import `from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey`. -
TypeError: 'bytes' object cannot be interpreted as an integer
cause Incorrect input type for cryptographic functions. X25519 operations typically expect 32-byte `bytes` objects for keys.fixEnsure all key material (private keys, public keys) are precisely 32-byte `bytes` objects. Avoid passing strings or integers directly.
Warnings
- breaking The GitHub repository linked in the PyPI metadata for this library is non-existent (404 Not Found), indicating the project is likely unmaintained and should not be used for new development.
- gotcha Pure Python cryptographic implementations, especially for operations like X25519, can be vulnerable to timing attacks due to variations in execution time based on input values. This can leak sensitive information.
- gotcha This `x25519` PyPI package is a distinct, pure-Python implementation and is NOT the X25519 implementation provided by the `cryptography` library, which is the standard and recommended choice for robust cryptography in Python.
- gotcha The raw shared secret derived from X25519 should generally not be used directly as an encryption key. It should be processed through a Key Derivation Function (KDF) like HKDF to produce a strong, fixed-length key suitable for symmetric encryption.
Install
-
pip install x25519
Imports
- scalar_base_mult
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
import x25519 public_key = x25519.scalar_base_mult(private_key)
- scalar_mult
private_key.exchange(peer_public_key)
import x25519 shared_secret = x25519.scalar_mult(private_key, peer_public_key)
Quickstart
import x25519
from binascii import hexlify
# Generate a 32-byte private key (randomly in a real application)
private_key_a = b'\x01' * 32 # Example: should be randomly generated
private_key_b = b'\x02' * 32 # Example: should be randomly generated
# Derive public keys
public_key_a = x25519.scalar_base_mult(private_key_a)
public_key_b = x25519.scalar_base_mult(private_key_b)
print(f"Public Key A: {hexlify(public_key_a).decode()}")
print(f"Public Key B: {hexlify(public_key_b).decode()}")
# Compute shared secrets
shared_secret_ab = x25519.scalar_mult(private_key_a, public_key_b)
shared_secret_ba = x25519.scalar_mult(private_key_b, public_key_a)
print(f"Shared Secret A->B: {hexlify(shared_secret_ab).decode()}")
print(f"Shared Secret B->A: {hexlify(shared_secret_ba).decode()}")
assert shared_secret_ab == shared_secret_ba
print("Shared secrets match!")