forked from dewan-ahmed/PassMeRust
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change main entry point in package.json to "passme.js" and update pas…
…sword checking logic to use in-memory dictionary and common password files instead of file I/O. - Update package.json main entry point to "passme.js" - Use in-memory dictionary and common password files for password checking instead of file I/O. The previous file I/O operations for reading the dictionary and common password files have been replaced with in-memory variables to improve performance. The new main entry point in package.json is "passme.js".
- Loading branch information
1 parent
6deab04
commit b27b312
Showing
2 changed files
with
16 additions
and
18 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
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,40 +1,38 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use neon::prelude::*; | ||
|
||
// Importing the entropy module | ||
mod entropy; | ||
|
||
const common_passwords: &str = include_str!("../data/common-passwords.txt"); | ||
const dictionary: &str = include_str!("../data/dictionary.txt"); | ||
|
||
fn check_password(mut cx: FunctionContext) -> JsResult<JsString> { | ||
let password = cx.argument::<JsString>(0)?.value(&mut cx) as String; | ||
let dictionary_path = Path::new("data/dictionary.txt"); | ||
let common_passwords_path = Path::new("data/common-passwords.txt"); | ||
if is_password_in_dictionary(&password, dictionary_path) | ||
|| is_password_common(&password, common_passwords_path) | ||
{ | ||
let message = "Password is weak: It matches a common password or dictionary word.".to_string(); | ||
return Ok(cx.string(message)) | ||
|
||
if is_password_in_dictionary(&password) || is_password_common(&password) { | ||
let message = | ||
"Password is weak: It matches a common password or dictionary word.".to_string(); | ||
return Ok(cx.string(message)); | ||
} else { | ||
// Use the imported calculate_entropy function | ||
let entropy = entropy::calculate_entropy(&password); | ||
let message = format!("Password strength: {}", entropy::get_strength(entropy)); | ||
return Ok(cx.string(message)) | ||
let message = format!("Password strength: {}", entropy::get_strength(entropy)); | ||
return Ok(cx.string(message)); | ||
} | ||
} | ||
|
||
#[neon::main] | ||
fn main(mut cx: ModuleContext) -> NeonResult<()>{ | ||
fn main(mut cx: ModuleContext) -> NeonResult<()> { | ||
println!("in main"); | ||
|
||
cx.export_function("check_password", check_password)?; | ||
Ok(()) | ||
|
||
} | ||
|
||
fn is_password_in_dictionary(password: &str, dictionary_path: &Path) -> bool { | ||
let dictionary = fs::read_to_string(dictionary_path).unwrap(); | ||
fn is_password_in_dictionary(password: &str) -> bool { | ||
dictionary.lines().any(|line| line == password) | ||
} | ||
|
||
fn is_password_common(password: &str, common_passwords_path: &Path) -> bool { | ||
let common_passwords = fs::read_to_string(common_passwords_path).unwrap(); | ||
fn is_password_common(password: &str) -> bool { | ||
common_passwords.lines().any(|line| line == password) | ||
} |