Skip to main content

encrypt_file/
encrypt_file.rs

1///////////////////////////////////////////////////////////////////////////////
2//
3// StringEncrypt WebApi interface usage example.
4//
5// In this example we will encrypt sample file with default options.
6//
7// Version        : v1.0.0
8// Language       : Rust
9// Author         : Bartosz Wójcik
10// Project page   : https://www.stringencrypt.com
11// Web page       : https://www.pelock.com
12//
13///////////////////////////////////////////////////////////////////////////////
14
15use std::path::PathBuf;
16
17use stringencrypt::{ErrorCode, Language, NewLine, StringEncrypt};
18
19#[tokio::main]
20async fn main() {
21    let mut string_encrypt = StringEncrypt::new("YOUR-API-KEY-HERE", false); // leave empty for demo mode
22    string_encrypt
23        .set_compression(false)
24        .set_unicode(true)
25        .set_lang_locale("en_US.utf8")
26        .set_new_lines(NewLine::LF)
27        .set_ansi_encoding("WINDOWS-1250")
28        .set_language(Language::PHP)
29        .set_cmd_min(1)
30        .set_cmd_max(3)
31        .set_local(false);
32
33    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
34    path.push("examples");
35    path.push("sample.bin");
36
37    // Full license: raw bytes from file (demo may return ERROR_DEMO).
38    let result = string_encrypt
39        .encrypt_file_contents(&path, "$label")
40        .await;
41
42    let Some(result) = result else {
43        println!("Cannot connect to the API or file is missing/empty.");
44        std::process::exit(1);
45    };
46
47    if result.error != Some(ErrorCode::SUCCESS as i64) {
48        println!("API error: {:?}", result.error);
49        std::process::exit(1);
50    }
51
52    println!("{}\n", result.source.unwrap_or_default());
53}