Rust
Rust
Find a JWE Recipient by Header Value
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.FindRecipient, which searches the per-recipient unprotected headers for a member with a given name and string value, and returns the matching recipient index.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. This locates the recipient that a particular key belongs to (for example by
kid) before decrypting. The method returns -1 when no match is found.Chilkat Rust Downloads
let jwe = chilkat::Jwe::new();
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load a multi-recipient JWE (JSON serialization) from a file.
let sb_jwe = chilkat::StringBuilder::new();
if sb_jwe.load_file("qa_data/message.jwe", "utf-8").is_err() {
println!("Failed to load the JWE file.");
return;
}
if jwe.load_jwe_sb(&sb_jwe).is_err() {
println!("{}", jwe.last_error_text());
return;
}
// Search the per-recipient unprotected headers for a member named "kid" whose value equals the
// given string. Returns the recipient index, or -1 if not found.
let b_case_sensitive = true;
let index = jwe.find_recipient("kid", "recipient-key-1", b_case_sensitive);
if index < 0 {
println!("Recipient not found.");
return;
}
println!("Found recipient at index {}", index);