Skip to content

Commit

Permalink
Merge pull request #91 from CodeDead/feature/include-symbols
Browse files Browse the repository at this point in the history
feature/include-symbols
  • Loading branch information
CodeDead authored Nov 28, 2023
2 parents 9af6d0a + b346707 commit 222922b
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 23 deletions.
17 changes: 14 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ fn main() {
open_website,
save_string_to_disk,
read_string_from_file,
generate_passwords
generate_passwords,
exit_app
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

#[tauri::command]
fn exit_app() {
std::process::exit(0x0);
}

#[tauri::command]
fn open_website(website: &str) -> Result<String, String> {
match open::that(website) {
Expand Down Expand Up @@ -48,12 +54,17 @@ async fn generate_passwords(
min_length: u64,
max_length: u64,
character_set: &str,
include_symbols: &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();

let mut total_character_set = String::from(character_set);
total_character_set.push_str(include_symbols);

let char_count = total_character_set.graphemes(true).count();

if !allow_duplicates {
let mut current = min_length;
Expand All @@ -64,7 +75,7 @@ async fn generate_passwords(
}

let mut rng = rand::thread_rng();
let chars = character_set.chars();
let chars = total_character_set.chars();
for _n in 0..amount {
let mut can_continue = false;
while !can_continue {
Expand Down
3 changes: 2 additions & 1 deletion src/components/CreatePasswordDialog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const CreatePasswordDialog = ({ open, onCreate, onClose }) => {
brackets,
useAdvanced,
characterSet,
includeSymbols,
allowDuplicates,
} = state2;

Expand Down Expand Up @@ -105,7 +106,7 @@ const CreatePasswordDialog = ({ open, onCreate, onClose }) => {
return;
}

generatePasswordArray(min, max, simpleCharacterSet, 1, allowDuplicates, worker)
generatePasswordArray(min, max, simpleCharacterSet, includeSymbols, 1, allowDuplicates, worker)
.then((res) => {
setPassword(res[0]);
})
Expand Down
3 changes: 2 additions & 1 deletion src/components/EditPasswordDialog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const EditPasswordDialog = ({
brackets,
useAdvanced,
characterSet,
includeSymbols,
allowDuplicates,
} = state2;

Expand Down Expand Up @@ -96,7 +97,7 @@ const EditPasswordDialog = ({
return;
}

generatePasswordArray(min, max, simpleCharacterSet, 1, allowDuplicates, null)
generatePasswordArray(min, max, simpleCharacterSet, includeSymbols, 1, allowDuplicates, null)
.then((res) => {
setPassword(res[0]);
})
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/PasswordContextProvider/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { createContext, useReducer } from 'react';
import PasswordReducer from '../../reducers/PasswordReducer';

const characterSet = localStorage.characterSet ? localStorage.characterSet : '';
const includeSymbols = localStorage.includeSymbols ? localStorage.includeSymbols : '';

const initState = {
length: 1,
Expand All @@ -15,6 +16,7 @@ const initState = {
numbers: true,
brackets: true,
characterSet,
includeSymbols,
useAdvanced: false,
passwords: null,
allowDuplicates: true,
Expand Down
3 changes: 2 additions & 1 deletion src/languages/de_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@
"confirmDelete": "Sind Sie sicher, dass Sie dieses Passwort löschen möchten?",
"hidePassword": "Passwort ausblenden",
"showPassword": "Passwort anzeigen",
"exit": "Beenden"
"exit": "Beenden",
"includeSymbols": "Symbole einschließen"
}
3 changes: 2 additions & 1 deletion src/languages/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@
"confirmDelete": "Are you sure you want to delete this password?",
"showPassword": "Show password",
"hidePassword": "Hide password",
"exit": "Exit"
"exit": "Exit",
"includeSymbols": "Include symbols"
}
3 changes: 2 additions & 1 deletion src/languages/fr_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@
"confirmDelete": "Êtes-vous sûr de vouloir supprimer cet élément ?",
"showPassword": "Afficher le mot de passe",
"hidePassword": "Masquer le mot de passe",
"exit": "Quitter"
"exit": "Quitter",
"includeSymbols": "Inclure les symboles"
}
3 changes: 2 additions & 1 deletion src/languages/jp_jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@
"confirmDelete": "このパスワードを削除してもよろしいですか?",
"showPassword": "パスワードを表示",
"hidePassword": "パスワードを隠す",
"exit": "出口"
"exit": "出口",
"includeSymbols": "シンボルを含める"
}
3 changes: 2 additions & 1 deletion src/languages/nl_nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,6 @@
"confirmDelete": "Weet je zeker dat je dit wachtwoord wilt verwijderen?",
"showPassword": "Wachtwoord tonen",
"hidePassword": "Wachtwoord verbergen",
"exit": "Afsluiten"
"exit": "Afsluiten",
"includeSymbols": "Inclusief symbolen"
}
3 changes: 2 additions & 1 deletion src/languages/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@
"confirmDelete": "Вы уверены, что хотите удалить этот элемент?",
"showPassword": "Показать пароль",
"hidePassword": "Скрыть пароль",
"exit": "Выход"
"exit": "Выход",
"includeSymbols": "Включить символы"
}
3 changes: 2 additions & 1 deletion src/languages/tr_tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@
"confirmDelete": "Bu şifreyi silmek istediğinizden emin misiniz?",
"showPassword": "Şifreyi göster",
"hidePassword": "Şifreyi gizle",
"exit": "Çıkış"
"exit": "Çıkış",
"includeSymbols": "Semboller dahil"
}
3 changes: 2 additions & 1 deletion src/languages/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@
"confirmDelete": "你确定要删除这个密码吗?",
"showPassword": "显示密码",
"hidePassword": "隐藏密码",
"exit": "退出"
"exit": "退出",
"includeSymbols": "包含符号"
}
1 change: 1 addition & 0 deletions src/reducers/PasswordReducer/Actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export const SET_PASSWORD_LENGTH_MIN = 'SET_PASSWORD_LENGTH_MIN';
export const SET_PASSWORD_LENGTH_MAX = 'SET_PASSWORD_LENGTH_MAX';
export const SET_PASSWORDS = 'SET_PASSWORDS';
export const SET_ALLOW_DUPLICATES = 'SET_ALLOW_DUPLICATES';
export const SET_INCLUDE_SYMBOLS = 'SET_INCLUDE_SYMBOLS';
11 changes: 9 additions & 2 deletions src/reducers/PasswordReducer/Actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SET_BRACKETS,
SET_CAPITAL_LETTERS,
SET_CHARACTER_SET,
SET_INCLUDE_SYMBOLS,
SET_NUMBERS,
SET_PASSWORD_AMOUNT,
SET_PASSWORD_LENGTH_MAX,
Expand Down Expand Up @@ -80,19 +81,25 @@ export const setAllowDuplicates = (value) => ({
payload: value,
});

export const setIncludeSymbols = (value) => ({
type: SET_INCLUDE_SYMBOLS,
payload: value,
});

// eslint-disable-next-line max-len
export const generatePasswordArray = (min, max, characterSet, amount, allowDuplicates, worker) => {
export const generatePasswordArray = (min, max, characterSet, includeSymbols, amount, allowDuplicates, worker) => {
// eslint-disable-next-line no-underscore-dangle
if (window.__TAURI__) {
return invoke('generate_passwords', {
minLength: parseFloat(min),
maxLength: parseFloat(max),
characterSet,
includeSymbols,
amount: parseFloat(amount),
allowDuplicates,
});
}
return worker.PasswordGenerator(min, max, characterSet, amount, allowDuplicates);
return worker.PasswordGenerator(min, max, characterSet, includeSymbols, amount, allowDuplicates);
};

// eslint-disable-next-line max-len
Expand Down
8 changes: 7 additions & 1 deletion src/reducers/PasswordReducer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
SET_ALLOW_DUPLICATES,
SET_BRACKETS,
SET_CAPITAL_LETTERS,
SET_CHARACTER_SET,
SET_CHARACTER_SET, SET_INCLUDE_SYMBOLS,
SET_NUMBERS,
SET_PASSWORD_AMOUNT,
SET_PASSWORD_LENGTH_MAX,
Expand Down Expand Up @@ -73,6 +73,12 @@ const PasswordReducer = (state, action) => {
...state,
characterSet: action.payload,
};
case SET_INCLUDE_SYMBOLS:
localStorage.includeSymbols = action.payload;
return {
...state,
includeSymbols: action.payload,
};
case SET_USE_ADVANCED:
return {
...state,
Expand Down
5 changes: 3 additions & 2 deletions src/routes/Generate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Generate = () => {
const [exportType, setExportType] = useState('application/json');

const {
min, max, amount, characterSet, passwords, useAdvanced, smallLetters,
min, max, amount, characterSet, includeSymbols, passwords, useAdvanced, smallLetters,
capitalLetters, spaces, specialCharacters, numbers, brackets, allowDuplicates,
} = state2;

Expand Down Expand Up @@ -61,7 +61,8 @@ const Generate = () => {
return;
}
d1(setLoading(true));
generatePasswordArray(min, max, simpleCharacterSet, amount, allowDuplicates, worker)
// eslint-disable-next-line max-len
generatePasswordArray(min, max, simpleCharacterSet, includeSymbols, amount, allowDuplicates, worker)
.then((res) => {
d2(setPasswords(res));
})
Expand Down
21 changes: 20 additions & 1 deletion src/routes/Home/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
setBrackets,
setCapitalLetters,
setCharacterSet,
setIncludeSymbols,
setNumbers,
setPasswordAmount,
setPasswordLengthMax,
Expand Down Expand Up @@ -59,6 +60,7 @@ const Home = () => {
brackets,
useAdvanced,
characterSet,
includeSymbols,
allowDuplicates,
} = state2;

Expand Down Expand Up @@ -88,7 +90,8 @@ const Home = () => {
}

d1(setLoading(true));
generatePasswordArray(min, max, simpleCharacterSet, amount, allowDuplicates, worker)
// eslint-disable-next-line max-len
generatePasswordArray(min, max, simpleCharacterSet, includeSymbols, amount, allowDuplicates, worker)
.then((res) => {
d2(setPasswords(res));
navigate('/generate');
Expand Down Expand Up @@ -147,6 +150,14 @@ const Home = () => {
d2(setCharacterSet(event.target.value));
};

/**
* Change the include symbols value
* @param event The event argument
*/
const handleIncludeSymbolsChange = (event) => {
d2(setIncludeSymbols(event.target.value));
};

useEffect(() => {
d1(setPageIndex(0));
}, []);
Expand Down Expand Up @@ -316,6 +327,14 @@ const Home = () => {
onChange={handleCharacterSetChange}
/>
</Grid>
<Grid item xs={12} md={12} lg={12}>
<TextField
label={language.includeSymbols}
value={includeSymbols}
fullWidth
onChange={handleIncludeSymbolsChange}
/>
</Grid>
</Grid>
</AccordionDetails>
</Accordion>
Expand Down
10 changes: 6 additions & 4 deletions src/utils/PasswordGenerator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ const getRandomIntInclusive = (min, max) => {
* @param minLength The minimum length of the password
* @param maxLength The maximum length of the password
* @param characterSet The character set to use
* @param includeSymbols Custom symbols to include
* @param amount The amount of passwords to generate
* @param allowDuplicates Whether to allow duplicate passwords
* @returns {*[]} The array of passwords
* @constructor
*/
// eslint-disable-next-line import/prefer-default-export
export const PasswordGenerator = (minLength, maxLength, characterSet, amount, allowDuplicates) => {
// eslint-disable-next-line import/prefer-default-export,max-len
export const PasswordGenerator = (minLength, maxLength, characterSet, includeSymbols, amount, allowDuplicates) => {
const passwordArray = [];
const totalCharacterSet = characterSet + includeSymbols;

let maxCount = 0;
if (!allowDuplicates) {
let current = parseInt(minLength, 10);
while (current <= parseInt(maxLength, 10)) {
// eslint-disable-next-line no-restricted-properties,prefer-exponentiation-operator
maxCount += Math.pow(characterSet.length, current);
maxCount += Math.pow(totalCharacterSet.length, current);
current += 1;
}
}
Expand All @@ -51,7 +53,7 @@ export const PasswordGenerator = (minLength, maxLength, characterSet, amount, al
window.crypto.getRandomValues(randomBuffer);

const randomNumber = randomBuffer[0] / (0xffffffff + 1);
password += characterSet[Math.floor(randomNumber * characterSet.length)];
password += totalCharacterSet[Math.floor(randomNumber * totalCharacterSet.length)];
}

if (allowDuplicates === true || (!allowDuplicates && !passwordArray.includes(password))) {
Expand Down

0 comments on commit 222922b

Please sign in to comment.