Sample code for 30+ languages & platforms
Rust

REST Basic Auth with Secure Strings

Demonstrates how to do REST Basic authentication using secure strings.

This example requires Chilkat v9.5.0.71 or greater.

Chilkat Rust Downloads

Rust

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

// Imagine we've previously saved our encrypted login and password within a JSON config file
// that contains this:

// {
//   "http_login": "mCrOmA7mBA7Au9RuJGb9hw==",
//   "http_password": "jJtiI9TgErTTpqBz9JtHBw=="
// }

let json = chilkat::JsonObject::new();
let _ = json.load_file("qa_data/passwords/http.json");

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

// These are the encryption settings we previously used to encrypt the credentials within the JSON config file.
crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("cbc");
crypt.set_key_length(128);
crypt.set_encoded_key("000102030405060708090A0B0C0D0E0F", "hex");
crypt.set_encoded_iv("000102030405060708090A0B0C0D0E0F", "hex");
crypt.set_encoding_mode("base64");

let ss_login = chilkat::SecureString::new();
let ss_password = chilkat::SecureString::new();

// Decrypt to the secure string.  (the strings will still held in memory encrypted, but are now encrypted using
// a randomly generated session key.)
let _ = crypt.decrypt_secure_enc(&json.string_of("http_login").unwrap_or_default(), &ss_login);
let _ = crypt.decrypt_secure_enc(&json.string_of("http_password").unwrap_or_default(), &ss_password);

let rest = chilkat::Rest::new();

// Connect to a REST server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("chilkatsoft.com", port, b_tls, b_auto_reconnect).is_ok();

// Cause the "Authorization: Basic ..." header to be added to HTTP requests
let _ = rest.set_auth_basic_secure(&ss_login, &ss_password);

let Ok(response_json) = rest.full_request_no_body("GET", "/helloWorld.html") else {
    println!("{}", rest.last_error_text());
    return;
};

// Show the LastRequestHeader that was sent.
println!("{}", rest.last_request_header());

// The LastRequestHeader looks like this:

// Host: chilkatsoft.com
// Authorization: Basic bXlIdHRwTG9naW46bXlIdHRwUGFzc3dvcmQ=