Merge pull request #5822 from mdickopp/mdickopp/pwGeneration
Improve client-side password generation
This commit is contained in:
commit
47a2204921
@ -181,11 +181,21 @@ export class UserRepositoryService extends BaseRepository<ViewUser, User, UserTi
|
|||||||
*/
|
*/
|
||||||
public getRandomPassword(length: number = 10): string {
|
public getRandomPassword(length: number = 10): string {
|
||||||
let pw = '';
|
let pw = '';
|
||||||
const array = new Uint8Array(length);
|
|
||||||
window.crypto.getRandomValues(array);
|
|
||||||
const characters = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
const characters = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
||||||
for (let i = 0; i < length; i++) {
|
// set charactersLengthPower2 to characters.length rounded up to the next power of two
|
||||||
pw += characters.charAt(array[i] % characters.length);
|
let charactersLengthPower2 = 1;
|
||||||
|
while (characters.length > charactersLengthPower2) {
|
||||||
|
charactersLengthPower2 *= 2;
|
||||||
|
}
|
||||||
|
while (pw.length < length) {
|
||||||
|
const random = new Uint8Array(length - pw.length);
|
||||||
|
window.crypto.getRandomValues(random);
|
||||||
|
for (let i = 0; i < random.length; i++) {
|
||||||
|
const r = random[i] % charactersLengthPower2;
|
||||||
|
if (r < characters.length) {
|
||||||
|
pw += characters.charAt(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return pw;
|
return pw;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user