Sample code for 30+ languages & platforms
Rust

DSA R,S Signature Values

See more DSA Examples

Creates a DSA signature. Gets r,s values from the signature. Re-creates the DSA signature ASN.1 from the r,s values. Then verifies the signature using the re-created ASN.1 DSA signature.

Chilkat Rust Downloads

Rust

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

let crypt = chilkat::Crypt2::new();

crypt.set_encoding_mode("hex");
crypt.set_hash_algorithm("sha-1");

let hash_str = crypt.hash_file_enc("qa_data/hamlet.xml").unwrap_or_default();
println!("hash to sign: {}", hash_str);

let dsa = chilkat::Dsa::new();

let pem_private_key = dsa.load_text("qa_data/dsa/dsaPrivKey2.pem").unwrap_or_default();
if dsa.from_pem(&pem_private_key).is_err() {
    println!("{}", dsa.last_error_text());
    return;
}

// Load the hash to be signed into the DSA object:
if dsa.set_encoded_hash("hex", &hash_str).is_err() {
    println!("{}", dsa.last_error_text());
    return;
}

// Sign the hash.
if dsa.sign_hash().is_err() {
    println!("{}", dsa.last_error_text());
    return;
}

// Get the ASN.1 signature.
let mut asn_sig = dsa.get_encoded_signature("base64").unwrap_or_default();
println!("Signature: {}", asn_sig);

// Examine the details of the ASN.1 signature.
// We want to get the r,s values as hex strings..
let asn = chilkat::Asn::new();
if asn.load_encoded(&asn_sig, "base64").is_err() {
    println!("{}", asn.last_error_text());
    return;
}

// Get the ASN.1 as XML.
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&asn.asn_to_xml().unwrap_or_default()).is_ok();
println!("Signature as XML: ");
println!("{}", xml.get_xml().unwrap_or_default());

// Sample XML shown here.
// The r and s values are the two hex strings in the XML.

// <?xml version="1.0" encoding="utf-8"?>
// <sequence>
//     <int>2C187F3AB6E47A66497B86CE97BB39E2133810F5</int>
//     <int>588E53D3F7B69636B48FD7175E99A3961BD7D775</int>
// </sequence>

// Pretend we're starting with r,s
let r = "2C187F3AB6E47A66497B86CE97BB39E2133810F5".to_string();
let s = "588E53D3F7B69636B48FD7175E99A3961BD7D775".to_string();

// Build the XML that will be converted to ASN.1
xml.clear();
xml.set_tag("sequence");
xml.new_child2("int", &r);
xml.new_child2("int", &s);

// Convert the XML to ASN.1
let _ = asn.load_asn_xml(&xml.get_xml().unwrap_or_default()).is_ok();

// Emit the signature as DER encoded ASN.1 (base64)
asn_sig = asn.get_encoded_der("base64").unwrap_or_default();

// --------------------------------------------------------------------
// Verify the signature using the asnSig we built from the r,s values
// --------------------------------------------------------------------

let dsa2 = chilkat::Dsa::new();

// Load the DSA public key to be used for verification:
let pem_public_key = dsa2.load_text("qa_data/dsa/dsaPubKey2.pem").unwrap_or_default();
if dsa2.from_public_pem(&pem_public_key).is_err() {
    println!("{}", dsa2.last_error_text());
    return;
}

// Load the hash to be verified.
if dsa2.set_encoded_hash("hex", &hash_str).is_err() {
    println!("{}", dsa2.last_error_text());
    return;
}

// Load the ASN.1 signature:
if dsa2.set_encoded_signature("base64", &asn_sig).is_err() {
    println!("{}", dsa2.last_error_text());
    return;
}

// Verify:
if dsa2.verify().is_err() {
    println!("{}", dsa2.last_error_text());
} else {
    println!("DSA Signature Verified!");
}