-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from CodeDead/feature/native-password-generator
Feature/native password generator
- Loading branch information
Showing
16 changed files
with
553 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,81 @@ | ||
#![cfg_attr( | ||
all(not(debug_assertions), target_os = "windows"), | ||
windows_subsystem = "windows" | ||
all(not(debug_assertions), target_os = "windows"), | ||
windows_subsystem = "windows" | ||
)] | ||
|
||
use rand::Rng; | ||
use std::fs; | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
fn main() { | ||
tauri::Builder::default() | ||
.invoke_handler(tauri::generate_handler![open_website, save_string_to_disk]) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
tauri::Builder::default() | ||
.invoke_handler(tauri::generate_handler![ | ||
open_website, | ||
save_string_to_disk, | ||
generate_passwords | ||
]) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
|
||
#[tauri::command] | ||
fn open_website(website: &str) -> Result<String, String> { | ||
match open::that(website) { | ||
Ok(_) => { | ||
Ok(String::from("Success")) | ||
match open::that(website) { | ||
Ok(_) => Ok(String::from("Success")), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
Err(e) => { | ||
Err(e.to_string()) | ||
} | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
fn save_string_to_disk(content: &str, path: &str) -> Result<String, String> { | ||
match fs::write(path, content) { | ||
Ok(_) => Ok(String::from("Success")), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
match fs::write(path, content) { | ||
Ok(_) => Ok(String::from("Success")), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
async fn generate_passwords( | ||
min_length: u64, | ||
max_length: u64, | ||
character_set: &str, | ||
amount: u64, | ||
allow_duplicates: bool, | ||
) -> Result<Vec<String>, String> { | ||
let mut password_list: Vec<String> = Vec::new(); | ||
let mut max_count: f64 = 0.0; | ||
let char_count = character_set.graphemes(true).count(); | ||
|
||
if !allow_duplicates { | ||
let mut current = min_length; | ||
while current <= max_length { | ||
max_count += (char_count as f64).powf(current as f64); | ||
current += 1; | ||
} | ||
} | ||
|
||
let mut rng = rand::thread_rng(); | ||
let chars = character_set.chars(); | ||
for _n in 0..amount { | ||
let mut can_continue = false; | ||
while !can_continue { | ||
let mut password = String::from(""); | ||
let length = rng.gen_range(min_length..(max_length + 1)); | ||
for _j in 0..length { | ||
let index = rng.gen_range(0..char_count); | ||
password.push(chars.clone().nth(index).unwrap()); | ||
} | ||
|
||
if allow_duplicates || (!allow_duplicates && !password_list.contains(&password)) { | ||
password_list.push(password); | ||
can_continue = true; | ||
} | ||
|
||
if !can_continue && !allow_duplicates && password_list.len() as f64 == max_count { | ||
return Ok(password_list); | ||
} | ||
} | ||
} | ||
|
||
Ok(password_list) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.