From 2b0cb6afe6789b5e83d41cc9da1fa415340d4421 Mon Sep 17 00:00:00 2001 From: Cyrmax Date: Sat, 2 Jul 2022 13:04:48 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 19 +++ src/main.rs | 141 ++++++++++++++++++++++ 4 files changed, 501 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d0fe100 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,340 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bumpalo" +version = "3.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "js-sys" +version = "0.3.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "libm" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "native-windows-derive" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76134ae81020d89d154f619fd2495a2cecad204276b1dc21174b55e4d0975edd" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "native-windows-gui" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e049bccae62e28782c5eff82e0c8a4dace50b9783fe03b95447fef3172bb89" +dependencies = [ + "bitflags", + "lazy_static", + "plotters", + "plotters-backend", + "stretch", + "winapi", + "winapi-build", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "plotters" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a3fd9ec30b9749ce28cd91f255d569591cdf937fe280c312143e3c4bad6f2a" +dependencies = [ + "num-traits", + "plotters-backend", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d88417318da0eaf0fdcdb51a0ee6c3bed624333bff8f946733049380be67ac1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rpg" +version = "0.1.0" +dependencies = [ + "native-windows-derive", + "native-windows-gui", + "rand", +] + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" + +[[package]] +name = "stretch" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b0dc6d20ce137f302edf90f9cd3d278866fd7fb139efca6f246161222ad6d87" +dependencies = [ + "lazy_static", + "libm", +] + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be" + +[[package]] +name = "web-sys" +version = "0.3.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fed94beee57daf8dd7d51f2b15dc2bcde92d7a72304cdf662a4371008b71b90" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c27b0c8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "rpg" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +native-windows-derive = { version = "1.0.5"} +native-windows-gui = { version = "1.0.12"} +rand = "0.8.5" + + +[profile.release] +strip = true +opt-level = "z" +lto = true +codegen-units = 1 +panic = "abort" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6a9e4e4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,141 @@ +#![windows_subsystem = "windows"] + +extern crate native_windows_gui as nwg; +extern crate native_windows_derive as nwd; + +use nwd::NwgUi; +use nwg::{NativeUi, CheckBoxState}; +use rand::prelude::*; + + +enum Condition { + Lowercase, Uppercase, Digit +} + + +#[derive(Default, NwgUi)] +pub struct RustPasswordGenerator { + + #[nwg_control(size: (300, 150), position: (300, 300), title: "Rust password generator")] + #[nwg_events( OnWindowClose: [ RustPasswordGenerator::exit ])] + window: nwg::Window, + + #[nwg_layout(parent: window, spacing: 5, min_size: [290, 140])] + grid: nwg::GridLayout, + + #[nwg_control(text: "Include uppercase letters", check_state: CheckBoxState::Checked)] + #[nwg_layout_item(layout: grid, col: 0, row: 0)] + upper_cb: nwg::CheckBox, + + #[nwg_control(text: "Include lowercase letters", check_state: CheckBoxState::Checked)] + #[nwg_layout_item(layout: grid, col: 1, row: 0)] + lower_cb: nwg::CheckBox, + + #[nwg_control(text: "Include numbers", check_state: CheckBoxState::Checked)] + #[nwg_layout_item(layout: grid, col: 2, row: 0)] + number_cb: nwg::CheckBox, + + #[nwg_control(text: "Password length")] + #[nwg_layout_item(layout: grid, col: 0, row: 1)] + pass_len_tb: nwg::Label, + + #[nwg_control(text: "12", flags: "VISIBLE|NUMBER", align: nwg::HTextAlign::Center, focus: true)] + #[nwg_layout_item(layout: grid, col: 1, row: 1, col_span: 3)] + passlen: nwg::TextInput, + + #[nwg_control(text: "Result")] + #[nwg_layout_item(layout: grid, col: 0, row: 2)] + result_tb: nwg::Label, + + #[nwg_control(text: "", align: nwg::HTextAlign::Center, readonly: true)] + #[nwg_layout_item(layout: grid, col: 1, row: 2, col_span: 5)] + result_text: nwg::TextInput, + + #[nwg_control(text: "Generate")] + #[nwg_layout_item(layout: grid, col: 4, row: 3)] + #[nwg_events( OnButtonClick: [ RustPasswordGenerator::generate_password ] )] + generate_btn: nwg::Button +} + + +impl RustPasswordGenerator { + fn generate_password(&self) { + let is_upper = state_to_bool(self.upper_cb.check_state()); + let is_lower = state_to_bool(self.lower_cb.check_state()); + let is_number = state_to_bool(self.number_cb.check_state()); + + if !(is_lower || is_upper || is_number) { + self.show_error("You should select at least one symbol group"); + return; + } + + if self.passlen.len() < 1 { + self.show_error("Password can not be 0 symbols in length"); + return; + } + + let pass_length: u32 = self.passlen.text().parse().unwrap(); + if pass_length < 1 { + self.show_error("Password length cannot be less than 1"); + return; + } + + let mut password = String::new(); + for _ in 0..pass_length { + password.push(get_random_symbol(is_upper, is_lower, is_number)); + } + self.result_text.set_text(&password); + } + + fn show_error(&self, message: &str) { + nwg::modal_error_message(&self.window, "error!", message); + } + + + fn exit(&self) { + nwg::stop_thread_dispatch(); + } +} + + +fn main() { + nwg::init().expect("failed to init nwg"); + nwg::Font::set_global_family("Segoe UI").expect("failed to set font"); + let _rpg = RustPasswordGenerator::build_ui(Default::default()).expect("failed to build UI"); + + nwg::dispatch_thread_events(); +} + + +fn get_random_symbol(include_upper: bool, include_lower: bool, include_num: bool) -> char { + let mut generator = rand::thread_rng(); + let mut choices: Vec = Vec::new(); + + if include_lower { + choices.push(Condition::Lowercase); + } + + if include_upper { + choices.push(Condition::Uppercase); + } + + if include_num { + choices.push(Condition::Digit); + } + + let choice = &choices[generator.gen_range(0..choices.len())]; + + match choice { + Condition::Digit => char::from(generator.gen_range(48..58)), + Condition::Lowercase => char::from(generator.gen_range(97..123)), + Condition::Uppercase => char::from(generator.gen_range(65..91)) + } +} + + +fn state_to_bool(state: nwg::CheckBoxState) -> bool { + match state { + CheckBoxState::Checked => true, + _ => false + } +} \ No newline at end of file