Rust
Rust
Sign a JWS with a Private Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPrivateKey, which sets the private key used to create a signature. The example uses RS256 (RSASSA-PKCS1-v1_5 with SHA-256).
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. Public-key JWS signs with a private key so that anyone holding the corresponding public key can verify the signature.
Chilkat Rust Downloads
let jws = chilkat::Jws::new();
// Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
let header = chilkat::JsonObject::new();
let _ = header.update_string("alg", "RS256");
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;
}
// 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 the signer's private key and set it for signature 0.
let priv_key = chilkat::PrivateKey::new();
if priv_key.load_pem_file("qa_data/signer_privkey.pem").is_err() {
println!("{}", priv_key.last_error_text());
return;
}
if jws.set_private_key(0, &priv_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);