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

Walmart Partner API Authentication (Generate a Signature for a Request)

See more RSA Examples

Demonstrates how to generate a signature for a Walmart Partner REST API call.

Chilkat Rust Downloads

Rust

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

let consumer_id = "b68d2a72....".to_string();
let base_url = "https://marketplace.walmartapis.com/v2/feeds".to_string();
// This is your Base64 encoded private key
let private_encoded_str = "MIICeAIBADANBgkqhkiG9w0BAQEFAA......".to_string();
let http_method = "GET".to_string();

// We need a timestamp in decimal string form representing the number of milliseconds since Jan 01 1970 UTC.
let dt = chilkat::DateTime::new();
// Set bLocal = true for a timestamp in the local timezone.  Set bLocal = false for a UTC timestamp.
let b_local = false;
// This gets the timestamp in seconds, not milliseconds.
let time_stamp_val = dt.get_as_unix_time(b_local) as i32;

// Build the string to sign.
let sb_string_to_sign = chilkat::StringBuilder::new();
let _ = sb_string_to_sign.append(&consumer_id);
let _ = sb_string_to_sign.append("\n");
let _ = sb_string_to_sign.append(&base_url);
let _ = sb_string_to_sign.append("\n");
let _ = sb_string_to_sign.append(&http_method);
let _ = sb_string_to_sign.append("\n");
let _ = sb_string_to_sign.append_int(time_stamp_val);
// We add three zero's so that the timestamp value is in milliseconds.
// We don't care about accuracy down to less than a second.
// All the server cares about is that the request was signed at the current date/time
// within some reasonable margin of error (to account for systems having clocks
// that may be slightly different).
let _ = sb_string_to_sign.append("000\n");

let priv_key = chilkat::PrivateKey::new();
// Load the private key into a private key object.
// Note: Technically the private key is not PEM because it lacks the header/footer strings
// used for PEM.  However, the LoadPem method will still accept it and load it correctly.
if priv_key.load_pem(&private_encoded_str).is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

let rsa = chilkat::Rsa::new();
if rsa.use_private_key(&priv_key).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// We want a base64 signature string.
rsa.set_encoding_mode("base64");

let Ok(signature_string) = rsa.sign_string_enc(&sb_string_to_sign.get_as_string().unwrap_or_default(), "SHA256") else {
    println!("{}", rsa.last_error_text());
    return;
};

println!("Signature String: {}", signature_string);