pub struct Encoder { /* private fields */ }Expand description
Builder for encoding Claim 169 credentials into QR-ready strings.
The encoder follows a builder pattern where configuration methods return Self
and the final encode() method consumes the builder to produce the result.
§Operation Order
When both signing and encryption are configured, the credential is always signed first, then encrypted (sign-then-encrypt), regardless of the order in which builder methods are called.
§Security
- Unsigned encoding requires explicit opt-in via
allow_unsigned() - Nonces are generated randomly by default for encryption
- Use explicit nonce methods only for testing or deterministic scenarios
§Example
use claim169_core::{Encoder, Claim169, CwtMeta};
let claim169 = Claim169::minimal("ID-001", "Jane Doe");
let cwt_meta = CwtMeta::new()
.with_issuer("https://issuer.example.com")
.with_expires_at(1800000000);
// Sign with Ed25519
let qr_data = Encoder::new(claim169, cwt_meta)
.sign_with_ed25519(&private_key)?
.encode()?;Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn sign_with<S: Signer + 'static>(
self,
signer: S,
algorithm: Algorithm,
) -> Self
pub fn sign_with<S: Signer + 'static>( self, signer: S, algorithm: Algorithm, ) -> Self
Sign with a custom signer implementation.
Use this method for HSM integration or custom cryptographic backends.
§Arguments
signer- A type implementing theSignertraitalgorithm- The COSE algorithm to use for signing
§Example
let encoder = Encoder::new(claim169, cwt_meta)
.sign_with(hsm_signer, iana::Algorithm::EdDSA);Sourcepub fn sign_with_ed25519(self, private_key: &[u8]) -> Result<Self>
pub fn sign_with_ed25519(self, private_key: &[u8]) -> Result<Self>
Sign with an Ed25519 private key.
The key is validated immediately and an error is returned if invalid.
§Arguments
private_key- 32-byte Ed25519 private key
§Errors
Returns an error if the private key is not exactly 32 bytes.
§Example
let encoder = Encoder::new(claim169, cwt_meta)
.sign_with_ed25519(&private_key)?;Sourcepub fn sign_with_ecdsa_p256(self, private_key: &[u8]) -> Result<Self>
pub fn sign_with_ecdsa_p256(self, private_key: &[u8]) -> Result<Self>
Sign with an ECDSA P-256 private key.
The key is validated immediately and an error is returned if invalid.
§Arguments
private_key- 32-byte ECDSA P-256 private key (scalar)
§Errors
Returns an error if the private key format is invalid.
§Example
let encoder = Encoder::new(claim169, cwt_meta)
.sign_with_ecdsa_p256(&private_key)?;Sourcepub fn encrypt_with<E: Encryptor + 'static>(
self,
encryptor: E,
algorithm: Algorithm,
) -> Self
pub fn encrypt_with<E: Encryptor + 'static>( self, encryptor: E, algorithm: Algorithm, ) -> Self
Sourcepub fn encrypt_with_aes256(self, key: &[u8]) -> Result<Self>
pub fn encrypt_with_aes256(self, key: &[u8]) -> Result<Self>
Encrypt with AES-256-GCM.
A random 12-byte nonce is generated automatically during encoding.
§Arguments
key- 32-byte AES-256 encryption key
§Errors
Returns an error if the key is not exactly 32 bytes.
§Example
let encoder = Encoder::new(claim169, cwt_meta)
.sign_with_ed25519(&sign_key)?
.encrypt_with_aes256(&aes_key)?;Sourcepub fn encrypt_with_aes128(self, key: &[u8]) -> Result<Self>
pub fn encrypt_with_aes128(self, key: &[u8]) -> Result<Self>
Sourcepub fn encrypt_with_aes256_nonce(
self,
key: &[u8],
nonce: &[u8; 12],
) -> Result<Self>
pub fn encrypt_with_aes256_nonce( self, key: &[u8], nonce: &[u8; 12], ) -> Result<Self>
Encrypt with AES-256-GCM using an explicit nonce.
Warning: Only use this for testing or deterministic scenarios. Reusing nonces with the same key is a critical security vulnerability.
§Arguments
key- 32-byte AES-256 encryption keynonce- 12-byte nonce/IV (must be unique per encryption)
Sourcepub fn encrypt_with_aes128_nonce(
self,
key: &[u8],
nonce: &[u8; 12],
) -> Result<Self>
pub fn encrypt_with_aes128_nonce( self, key: &[u8], nonce: &[u8; 12], ) -> Result<Self>
Encrypt with AES-128-GCM using an explicit nonce.
Warning: Only use this for testing or deterministic scenarios. Reusing nonces with the same key is a critical security vulnerability.
§Arguments
key- 16-byte AES-128 encryption keynonce- 12-byte nonce/IV (must be unique per encryption)
Sourcepub fn allow_unsigned(self) -> Self
pub fn allow_unsigned(self) -> Self
Sourcepub fn skip_biometrics(self) -> Self
pub fn skip_biometrics(self) -> Self
Sourcepub fn encode(self) -> Result<String>
pub fn encode(self) -> Result<String>
Encode the credential to a Base45 QR string.
This method consumes the encoder and produces the final QR-ready string.
§Pipeline
Claim169 → CBOR → CWT → COSE_Sign1 → [COSE_Encrypt0] → zlib → Base45§Errors
Returns an error if:
- Neither a signer nor
allow_unsigned()was configured - Signing fails
- Encryption fails
- CBOR encoding fails
§Example
let qr_data = Encoder::new(claim169, cwt_meta)
.sign_with_ed25519(&key)?
.encode()?;
println!("QR content: {}", qr_data);