-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated generatePassword function #9475
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,15 +86,15 @@ function sortByDate(aRaw: any, bRaw: any, columnKey: string, sortDirection: numb | |
aRaw[columnKey] === null | ||
? null | ||
: aRaw[columnKey] instanceof Date | ||
? aRaw[columnKey] | ||
: // eslint-disable-next-line local-rules/no-raw-date | ||
? aRaw[columnKey] | ||
: // eslint-disable-next-line local-rules/no-raw-date | ||
new Date(aRaw[columnKey].replace(unparsableDateRegex, "$1")); | ||
const bDate = | ||
bRaw[columnKey] === null | ||
? null | ||
: bRaw[columnKey] instanceof Date | ||
? bRaw[columnKey] | ||
: // eslint-disable-next-line local-rules/no-raw-date | ||
? bRaw[columnKey] | ||
: // eslint-disable-next-line local-rules/no-raw-date | ||
new Date(bRaw[columnKey].replace(unparsableDateRegex, "$1")); | ||
|
||
const result = aDate > bDate ? 1 : aDate < bDate ? -1 : 0; | ||
|
@@ -116,16 +116,34 @@ function capitalize(str: string): string { | |
} | ||
|
||
function generatePassword(): string { | ||
const length = Math.floor(Math.random() * 10) + 15; | ||
// See https://stackoverflow.com/a/68617567/1470607 | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:-_"; | ||
const array = new Uint32Array(charset.length); | ||
window.crypto.getRandomValues(array); | ||
const length = 24; // fixed length for better security | ||
const lowercase = "abcdefghijklmnopqrstuvwxyz"; | ||
const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
const numbers = "0123456789"; | ||
const specials = ":-_!@#$%^&*()"; // extended set of special characters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just out of curiosity, why does this constant only include this limited amount of special chars? |
||
|
||
// Helper function to get a random character from a given set | ||
const allChars = lowercase + uppercase + numbers + specials; | ||
|
||
// Ensure at least one character from each category | ||
const getRandomChar = (charset) => charset[Math.floor(Math.random() * charset.length)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
let password = ""; | ||
for (let i = 0; i < length; i++) { | ||
password += charset[array[i] % charset.length]; | ||
password += getRandomChar(lowercase); | ||
password += getRandomChar(uppercase); | ||
password += getRandomChar(numbers); | ||
password += getRandomChar(specials); | ||
|
||
const array = new Uint8Array(length - 4); // Remaining characters | ||
window.crypto.getRandomValues(array); | ||
|
||
for (let i = 0; i < array.length; i++) { | ||
password += allChars[array[i] % allChars.length]; | ||
} | ||
|
||
// Shuffle the password to avoid guaranteed character positions | ||
password = password.split('').sort(() => 0.5 - Math.random()).join(''); | ||
Comment on lines
+144
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For shuffling, you could also use the Fisher–Yates shuffle. |
||
|
||
return password; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Improvement in generatePassword function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not true? Using a fixed length reduces security, since it makes the space you need to brute force smaller. To exemplify, if we use only passwords of length 25, then there are
X^25
combinations whereX
is the number of symbols in our charset, but if we allow for example passwords of length 15-25, then there areX^15 + X^16 + X^17 + ... + X^25
combinations.