Skip to content

Commit

Permalink
word update
Browse files Browse the repository at this point in the history
  • Loading branch information
gcrois committed Sep 27, 2024
1 parent 52c3fc8 commit 6c2a04f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"astro": "^2.5.5",
"qrcode.react": "^4.0.1",
"randexp": "^0.5.3",
"random-words": "^2.0.1",
"react": "^19.0.0-beta",
"react-dom": "^19.0.0-beta",
"sanitize-filename": "^1.6.3",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 49 additions & 32 deletions src/utils/password.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
import React, { useState } from 'react';
import RandExp from 'randexp';
import { generate } from 'random-words';

const regexOptions = [
{
value: 'strong',
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=.{12,})[A-Za-z\\d@#$%^&+=!]{12,32}$',
label: 'Strong Password',
description: 'At least 12 characters, max 32. Includes uppercase, lowercase, number, and special character.'
},
{
value: 'very-strong',
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=.{16,})[A-Za-z\\d@#$%^&+=!]{16,64}$',
label: 'Very Strong Password',
description: 'At least 16 characters, max 64. Includes uppercase, lowercase, number, and special character.'
},
{
value: 'memorable',
regex: '^($word){3}\\d{2,4}[@#$%^&+=!]{1,2}$', // Use custom $word token here
label: 'Memorable Password',
description: '3 random words, followed by 2-4 digits and 1-2 special characters.'
},
{
value: 'pin',
regex: '\\d{6}',
label: 'PIN',
description: '6-digit PIN number.'
},
];

// Function to replace custom tokens after the base password is generated
const transformCustomTokens = (generatedString: string): string => {
// Replace each occurrence of $word with a random capitalized word
return generatedString.replace(/word/g, () => {
const word = generate({ exactly: 1, maxLength: 12 })[0];
return word.charAt(0).toUpperCase() + word.slice(1);
});
};

const PasswordGenerator = () => {
const [password, setPassword] = useState('');
const [currentRegex, setCurrentRegex] = useState('^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=.{12,})[A-Za-z\\d@#$%^&+=!]{12,64}$');
const [currentRegex, setCurrentRegex] = useState(regexOptions[0].regex);
const [selectedOption, setSelectedOption] = useState('strong');

const regexOptions = [
{
value: 'strong',
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=.{12,})[A-Za-z\\d@#$%^&+=!]{12,64}$',
label: 'Strong Password',
description: 'At least 12 characters, max 64. Includes uppercase, lowercase, number, and special character.'
},
{
value: 'very-strong',
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=.{16,})[A-Za-z\\d@#$%^&+=!]{16,128}$',
label: 'Very Strong Password',
description: 'At least 16 characters, max 128. Includes uppercase, lowercase, number, and special character.'
},
{
value: 'memorable',
regex: '^([A-Z][a-z]{6,12}){3}\\d{2,4}[@#$%^&+=!]{1,2}$',
label: 'Memorable Password',
description: '3 capitalized words (6-12 chars each), followed by 2-4 digits and 1-2 special characters.'
},
{
value: 'pin',
regex: '\\d{6}',
label: 'PIN',
description: '6-digit PIN number.'
},
];

const generatePassword = () => {
try {
// Generate the base string using RandExp
const randexp = new RandExp(new RegExp(currentRegex));
setPassword(randexp.gen());
let basePassword = randexp.gen();

// Apply custom transformations to the generated base string
const transformedPassword = transformCustomTokens(basePassword);

// Set the final password
setPassword(transformedPassword);
} catch (error) {
setPassword('Invalid regex pattern');
}
Expand All @@ -49,8 +66,8 @@ const PasswordGenerator = () => {

const handleOptionChange = (e) => {
const selected = regexOptions.find(option => option.value === e.target.value);
setSelectedOption(selected.value);
setCurrentRegex(selected.regex);
setSelectedOption(selected?.value || 'custom');
setCurrentRegex(selected?.regex || '');
};

return (
Expand Down Expand Up @@ -112,4 +129,4 @@ const PasswordGenerator = () => {
);
};

export default PasswordGenerator;
export default PasswordGenerator;

0 comments on commit 6c2a04f

Please sign in to comment.