python_obfuscator_example_simple/python-obfuscator-example-simple.rs
1/******************************************************************************
2 * Python Obfuscator WebApi interface usage example.
3 *
4 * In this example we will obfuscate sample source with default options.
5 *
6 * Version : v1.0.0
7 * Language : Rust
8 * Author : Bartosz Wójcik
9 * Web page : https://www.pelock.com
10 *
11 *****************************************************************************/
12
13use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
14
15#[tokio::main]
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 //
18 // include Python Obfuscator class
19 //
20
21 //
22 // when developing from a repository clone without installing the package use:
23 //
24 // use python_obfuscator::{PythonObfuscator, PythonObfuscatorResponse};
25 // (path dependency in Cargo.toml pointing to this crate)
26
27 //
28 // create Python Obfuscator class instance (we are using our activation key)
29 //
30 let my_python_obfuscator =
31 PythonObfuscator::new(Some("ABCD-ABCD-ABCD-ABCD".to_string()));
32
33 //
34 // source code in Python format
35 //
36 let script_source_code = r#"label = 'SecretKey'
37port = 443
38
39def get_sum(a, b):
40 return a + b
41
42print(label, port)
43r = get_sum(11, 31)
44print(r)"#;
45
46 //
47 // by default all obfuscation options are enabled, so we can just simply call:
48 //
49 let result = my_python_obfuscator
50 .obfuscate_script_source(script_source_code, true)
51 .await;
52
53 //
54 // it's also possible to pass a Python script file path instead of a string e.g.
55 //
56 // let result = my_python_obfuscator
57 // .obfuscate_script_file(std::path::Path::new("/path/to/project/script.py"), true)
58 // .await;
59
60 //
61 // result object holds the obfuscation results as well as other information
62 //
63 // result.error - error code
64 // result.output - obfuscated code
65 // result.demo - demo mode (empty/invalid key); strategy flags ignored; always integers_to_arrays, mba_binops, encrypt_strings (no virt; 1000 char limit)
66 // result.license_expiration - license end date (Y-m-d), empty if none
67 // result.usages_total - total obfuscations for this activation code
68 //
69 match result {
70 Some(PythonObfuscatorResponse::Object(obj)) => {
71 //
72 // display obfuscated code
73 //
74 if obj.error == PythonObfuscator::ERROR_SUCCESS {
75 println!("{}", obj.output.unwrap_or_default());
76 } else {
77 return Err(
78 format!("An error occurred, error code: {}", obj.error).into(),
79 );
80 }
81 }
82 Some(PythonObfuscatorResponse::Json(_)) => {}
83 None => {
84 return Err("Something unexpected happen while trying to obfuscate the code.".into());
85 }
86 }
87
88 Ok(())
89}