Sample code for 30+ languages & platforms
Rust

IMAP Login Secure

Demonstrates how to use the LoginSecure method introduced in Chilkat v9.5.0.71.

Chilkat Rust Downloads

Rust

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

let imap = chilkat::Imap::new();

// Connect to an IMAP server..
imap.set_ssl(true);
imap.set_port(993);
if imap.connect("imap.mail.me.com").is_err() {
    println!("{}", imap.last_error_text());
    return;
}

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

// {
//   "imap_login": "fLkxsfnVaIWLiL/R32jo6g==",
//   "imap_password": "/NbaFBoCftBLFf8WBU9Xtw=="
// }

let json = chilkat::JsonObject::new();
let _ = json.load_file("qa_data/passwords/imap.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("imap_login").unwrap_or_default(), &ss_login);
let _ = crypt.decrypt_secure_enc(&json.string_of("imap_password").unwrap_or_default(), &ss_password);

// Pass the credentials to the LoginSecure method:
if imap.login_secure(&ss_login, &ss_password).is_err() {
    println!("{}", imap.last_error_text());
    return;
}

// ...

let _ = imap.disconnect();