Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Validate a JWS Using ECDSA P-521 SHA-512

See more JSON Web Signatures (JWS) Examples

Validates a JSON Web Signature (JWS) that uses ECDSA P-521 SHA-512

Chilkat Rust Downloads

Rust

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example takes a JSON signature in compact serialization format,
// and uses an ECDSA public key to validate and recover the protected header and payload.

// We only need a public key for signature validation.  This is the ECDSA public key
// that is used:

//      {"kty":"EC", 
//       "crv":"P-521", 
//       "x":"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk", 
//       "y":"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDly79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2" 
//      } 

let sb_pub_key = chilkat::StringBuilder::new();
let _ = sb_pub_key.append("{\"kty\":\"EC\",");
let _ = sb_pub_key.append("\"crv\":\"P-521\",");
let _ = sb_pub_key.append("\"x\":\"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk\",");
let _ = sb_pub_key.append("\"y\":\"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDly79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2\"");
let _ = sb_pub_key.append("}");

let pub_key = chilkat::PublicKey::new();
if pub_key.load_from_string(&sb_pub_key.get_as_string().unwrap_or_default()).is_err() {
    println!("{}", pub_key.last_error_text());
    return;
}

let jws = chilkat::Jws::new();

// Set the ECC public key:
let signature_index = 0;
let _ = jws.set_public_key(signature_index, &pub_key);

// Load the JWS.
let sb_jws = chilkat::StringBuilder::new();
let _ = sb_jws.append("eyJhbGciOiJFUzUxMiJ9");
let _ = sb_jws.append(".");
let _ = sb_jws.append("UGF5bG9hZA");
let _ = sb_jws.append(".");
let _ = sb_jws.append("AdwMgeerwtHoh-l192l60hp9wAHZFVJbLfD_UxMi70cwnZOYaRI1bKPWROc-mZZq");
let _ = sb_jws.append("wqT2SI-KGDKB34XO0aw_7XdtAG8GaSwFKdCAPZgoXD2YBJZCPEX3xKpRwcdOO8Kp");
let _ = sb_jws.append("EHwJjyqOgzDO7iKvU8vcnwNrmxYbSW9ERBXukOXolLzeO_Jn");

if jws.load_jws_sb(&sb_jws).is_err() {
    println!("{}", jws.last_error_text());
    return;
}

// Validate the 1st (and only) signature at index 0..
let v = jws.validate(signature_index);
if v < 0 {
    // Perhaps Chilkat was not unlocked or the trial expired..
    println!("Method call failed for some other reason.");
    println!("{}", jws.last_error_text());
    return;
}

if v == 0 {
    println!("Invalid signature.  The ECC key was incorrect, the JWS was invalid, or both.");
    return;
}

// If we get here, the signature was validated..
println!("Signature validated.");
println!("--");

// Recover the original content:
println!("Recovered content:");
println!("{}", jws.get_payload("utf-8").unwrap_or_default());
println!("--");

// Examine the protected header:

let jose_header = chilkat::JsonObject::new();
if jws.get_protected_h(signature_index, &jose_header).is_err() {
    println!("{}", jws.last_error_text());
    return;
}

jose_header.set_emit_compact(false);

println!("Protected (JOSE) header:");
println!("{}", jose_header.emit().unwrap_or_default());

// Output:
// (the string "Payload" was the content that was signed.)

// 	Signature validated.
// 	--
// 	Recovered content:
// 	Payload
// 	--
// 	Protected (JOSE) header:
// 	{
// 	  "alg": "ES512"
// 	}