Skip to content

Commit f0bfa45

Browse files
committed
Fixed an issue that caused incorrect number parsing, generate passwords when clicking on generate buttons
1 parent 45c3cb5 commit f0bfa45

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed

src/routes/Advanced/index.jsx

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,73 @@ import { useNavigate } from 'react-router-dom';
99
import FormControlLabel from '@mui/material/FormControlLabel';
1010
import Checkbox from '@mui/material/Checkbox';
1111
import FormGroup from '@mui/material/FormGroup';
12-
import { setPageIndex } from '../../reducers/MainReducer/Actions';
12+
import { createWorkerFactory, useWorker } from '@shopify/react-web-worker';
13+
import { setError, setPageIndex } from '../../reducers/MainReducer/Actions';
1314
import { MainContext } from '../../contexts/MainContextProvider';
1415
import { PasswordContext } from '../../contexts/PasswordContextProvider';
1516
import {
1617
setAllowDuplicates,
17-
setCharacterSet,
18+
setCharacterSet, setPasswords,
1819
setUseAdvanced,
1920
} from '../../reducers/PasswordReducer/Actions';
2021

22+
const createWorker = createWorkerFactory(() => import('../../utils/PasswordGenerator/index'));
23+
2124
const Advanced = () => {
2225
const [state1, d1] = useContext(MainContext);
2326
const [state2, d2] = useContext(PasswordContext);
2427

2528
const { languageIndex } = state1;
26-
const { characterSet, useAdvanced, allowDuplicates } = state2;
29+
const {
30+
characterSet, useAdvanced, allowDuplicates, smallLetters, capitalLetters, spaces,
31+
specialCharacters, numbers, brackets, min, max, amount,
32+
} = state2;
2733

2834
const language = state1.languages[languageIndex];
35+
2936
const navigate = useNavigate();
37+
const worker = useWorker(createWorker);
38+
39+
/**
40+
* Generate passwords
41+
*/
42+
const generatePasswords = () => {
43+
let simpleCharacterSet = characterSet;
44+
if (!useAdvanced) {
45+
simpleCharacterSet = '';
46+
if (smallLetters) {
47+
simpleCharacterSet += 'abcdefghijklmnopqrstuvwxyz';
48+
}
49+
if (capitalLetters) {
50+
simpleCharacterSet += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
51+
}
52+
if (spaces) {
53+
simpleCharacterSet += ' ';
54+
}
55+
if (specialCharacters) {
56+
simpleCharacterSet += '=+-_!?.,;:\'\\"/%^*$€£&µ@#';
57+
}
58+
if (numbers) {
59+
simpleCharacterSet += '0123456789';
60+
}
61+
if (brackets) {
62+
simpleCharacterSet += '[]{}()<>';
63+
}
64+
}
65+
66+
if (!simpleCharacterSet || simpleCharacterSet.length === 0 || min > max || max < min) {
67+
return;
68+
}
69+
70+
worker.PasswordGenerator(min, max, simpleCharacterSet, amount, allowDuplicates)
71+
.then((res) => {
72+
d2(setPasswords(res));
73+
navigate('/generate');
74+
})
75+
.catch((e) => {
76+
d1(setError(e));
77+
});
78+
};
3079

3180
useEffect(() => {
3281
d1(setPageIndex(1));
@@ -85,7 +134,7 @@ const Advanced = () => {
85134
color="primary"
86135
style={{ float: 'right' }}
87136
sx={{ mt: 2 }}
88-
onClick={() => navigate('/generate')}
137+
onClick={generatePasswords}
89138
>
90139
{language.generate}
91140
</Button>

src/routes/Home/index.jsx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ import FormGroup from '@mui/material/FormGroup';
99
import Grid from '@mui/material/Grid';
1010
import TextField from '@mui/material/TextField';
1111
import { useNavigate } from 'react-router-dom';
12+
import { createWorkerFactory, useWorker } from '@shopify/react-web-worker';
1213
import { MainContext } from '../../contexts/MainContextProvider';
13-
import { setPageIndex } from '../../reducers/MainReducer/Actions';
14+
import { setError, setPageIndex } from '../../reducers/MainReducer/Actions';
1415
import { PasswordContext } from '../../contexts/PasswordContextProvider';
1516
import {
1617
setBrackets,
1718
setCapitalLetters,
1819
setNumbers,
1920
setPasswordAmount,
2021
setPasswordLengthMax,
21-
setPasswordLengthMin,
22+
setPasswordLengthMin, setPasswords,
2223
setSmallLetters,
2324
setSpaces,
2425
setSpecialCharacters,
2526
} from '../../reducers/PasswordReducer/Actions';
2627

28+
const createWorker = createWorkerFactory(() => import('../../utils/PasswordGenerator/index'));
29+
2730
const Home = () => {
2831
const [state, d1] = useContext(MainContext);
2932
const [state2, d2] = useContext(PasswordContext);
@@ -42,9 +45,53 @@ const Home = () => {
4245
numbers,
4346
brackets,
4447
useAdvanced,
48+
characterSet,
49+
allowDuplicates,
4550
} = state2;
4651

4752
const navigate = useNavigate();
53+
const worker = useWorker(createWorker);
54+
55+
/**
56+
* Generate passwords
57+
*/
58+
const generatePasswords = () => {
59+
let simpleCharacterSet = characterSet;
60+
if (!useAdvanced) {
61+
simpleCharacterSet = '';
62+
if (smallLetters) {
63+
simpleCharacterSet += 'abcdefghijklmnopqrstuvwxyz';
64+
}
65+
if (capitalLetters) {
66+
simpleCharacterSet += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
67+
}
68+
if (spaces) {
69+
simpleCharacterSet += ' ';
70+
}
71+
if (specialCharacters) {
72+
simpleCharacterSet += '=+-_!?.,;:\'\\"/%^*$€£&µ@#';
73+
}
74+
if (numbers) {
75+
simpleCharacterSet += '0123456789';
76+
}
77+
if (brackets) {
78+
simpleCharacterSet += '[]{}()<>';
79+
}
80+
}
81+
82+
if (!simpleCharacterSet || simpleCharacterSet.length === 0 || min > max || max < min) {
83+
return;
84+
}
85+
86+
worker.PasswordGenerator(min, max, simpleCharacterSet, amount, allowDuplicates)
87+
.then((res) => {
88+
d2(setPasswords(res));
89+
navigate('/generate');
90+
})
91+
.catch((e) => {
92+
d1(setError(e));
93+
});
94+
};
4895

4996
useEffect(() => {
5097
d1(setPageIndex(0));
@@ -162,7 +209,7 @@ const Home = () => {
162209
color="primary"
163210
style={{ float: 'right' }}
164211
sx={{ mt: 2, ml: 2 }}
165-
onClick={() => navigate('/generate')}
212+
onClick={generatePasswords}
166213
>
167214
{language.generate}
168215
</Button>

src/utils/PasswordGenerator/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/**
2+
* Generate a random number between min and max
3+
* @param min The minimum
4+
* @param max The maximum
5+
* @returns {number} The random number between min and max (inclusive)
6+
*/
7+
const getRandomInt = (min, max) => {
8+
const newMin = Math.ceil(min);
9+
const newMax = Math.floor(max);
10+
return Math.floor(Math.random() * (newMax - newMin + 1)) + newMin;
11+
};
12+
113
/**
214
* Generate an array of passwords
315
* @param minLength The minimum length of the password
@@ -14,8 +26,8 @@ export function PasswordGenerator(minLength, maxLength, characterSet, amount, al
1426

1527
let maxCount = 0;
1628
if (!allowDuplicates) {
17-
let current = minLength;
18-
while (current <= maxLength) {
29+
let current = parseInt(minLength, 10);
30+
while (current <= parseInt(maxLength, 10)) {
1931
// eslint-disable-next-line no-restricted-properties,prefer-exponentiation-operator
2032
maxCount += Math.pow(characterSet.length, current);
2133
current += 1;
@@ -27,7 +39,7 @@ export function PasswordGenerator(minLength, maxLength, characterSet, amount, al
2739

2840
while (!canContinue) {
2941
let password = '';
30-
const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
42+
const length = getRandomInt(minLength, maxLength);
3143
for (let j = 0; j < length; j += 1) {
3244
password += characterSet[Math.floor(Math.random() * characterSet.length)];
3345
}

0 commit comments

Comments
 (0)