Rust
Rust
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat Rust Downloads
let jws = chilkat::Jws::new();
// Protected header specifying HMAC-SHA256.
let header = chilkat::JsonObject::new();
let _ = header.update_string("alg", "HS256");
if jws.set_protected_header(0, &header).is_err() {
println!("{}", jws.last_error_text());
return;
}
// Set the optional unprotected header for signature 0. Unlike the protected header, it is not
// covered by the signature. It is carried in the JSON serialization forms of a JWS.
let unprotected = chilkat::JsonObject::new();
let _ = unprotected.update_string("kid", "signer-key-1");
if jws.set_unprotected_header(0, &unprotected).is_err() {
println!("{}", jws.last_error_text());
return;
}
let b_include_bom = false;
if jws.set_payload("This is the content to sign.", "utf-8", b_include_bom).is_err() {
println!("{}", jws.last_error_text());
return;
}
let base64_key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=".to_string();
if jws.set_mac_key(0, &base64_key, "base64").is_err() {
println!("{}", jws.last_error_text());
return;
}
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
let Ok(jws_json) = jws.create_jws() else {
println!("{}", jws.last_error_text());
return;
};
println!("{}", jws_json);