Sample code for 30+ languages & platforms
Rust

Sign a JWS with an HMAC Key from BinData

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.

Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.

Chilkat Rust Downloads

Rust

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;
}

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;
}

// Set the symmetric MAC key for signature 0 from the raw bytes in a BinData.  In production, obtain
// the key material from a secure source.
let bd_key = chilkat::BinData::new();
let _ = bd_key.append_encoded("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=", "base64");
if jws.set_mac_key_bd(0, &bd_key).is_err() {
    println!("{}", jws.last_error_text());
    return;
}

let Ok(jws_compact) = jws.create_jws() else {
    println!("{}", jws.last_error_text());
    return;
};

println!("{}", jws_compact);