Rsa
provide.foundation.crypto.rsa
¶
RSA digital signature implementation.
RSA-PSS signatures with SHA-256 for compatibility with existing systems. For new applications, prefer Ed25519 (faster, smaller keys, simpler).
Examples:
>>> signer = RSASigner.generate(key_size=2048)
>>> signature = signer.sign(b"message")
>>> verifier = RSAVerifier(signer.public_key_pem)
>>> assert verifier.verify(b"message", signature)
Classes¶
RSASigner
¶
RSA digital signature signer.
Stateful signer for RSA-PSS signatures. Use Ed25519Signer for new applications; RSA is provided for compatibility with existing systems.
Examples:
Generate new keypair: >>> signer = RSASigner.generate(key_size=2048) >>> signature = signer.sign(b"message") >>> public_pem = signer.public_key_pem
Load existing key: >>> signer = RSASigner(private_key_pem=existing_pem) >>> signature = signer.sign(b"message")
Attributes¶
public_key_pem
cached
property
¶
Functions¶
__attrs_post_init__
¶
Initialize private key object from PEM.
Source code in provide/foundation/crypto/rsa.py
export_private_key_pem
¶
Export private key in PEM format.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
PEM-encoded private key |
Warning
Private keys should be stored securely. Consider encryption.
Source code in provide/foundation/crypto/rsa.py
generate
classmethod
¶
Generate new signer with random RSA keypair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_size
|
int
|
RSA key size in bits (2048, 3072, or 4096) |
DEFAULT_RSA_KEY_SIZE
|
Returns:
| Name | Type | Description |
|---|---|---|
RSASigner |
Self
|
Signer with newly generated keypair |
Source code in provide/foundation/crypto/rsa.py
sign
¶
Sign data with RSA-PSS.
Uses PSS padding with SHA-256 hash, which is the modern recommended RSA signature scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to sign |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
RSA-PSS signature |
Raises:
| Type | Description |
|---|---|
CryptoSignatureError
|
If signature generation fails |
Source code in provide/foundation/crypto/rsa.py
RSAVerifier
¶
RSA signature verifier.
Stateful verifier for RSA-PSS signatures.
Examples:
>>> signer = RSASigner.generate(key_size=2048)
>>> verifier = RSAVerifier(signer.public_key_pem)
>>> signature = signer.sign(b"message")
>>> assert verifier.verify(b"message", signature)
Functions¶
__attrs_post_init__
¶
Initialize public key object from PEM.
Source code in provide/foundation/crypto/rsa.py
verify
¶
Verify RSA-PSS signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data that was signed |
required |
signature
|
bytes
|
RSA-PSS signature |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if signature is valid, False otherwise |