Skip to content

Commit

Permalink
Change main entry point in package.json to "passme.js" and update pas…
Browse files Browse the repository at this point in the history
…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
codegod100 committed Sep 9, 2023
1 parent 6deab04 commit b27b312
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "pass_me_rust",
"version": "0.1.0",
"description": "",
"main": "index.node",
"main": "passme.js",
"scripts": {
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics --future-incompat-report",
"build-debug": "npm run build --",
Expand Down
32 changes: 15 additions & 17 deletions src/lib.rs
@@ -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)
}

0 comments on commit b27b312

Please sign in to comment.