fix(files_sharing): fallback self.crypto.getRandomValues

Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
pull/53635/head
skjnldsv 2025-06-21 10:08:08 +07:00 committed by John Molakvoæ
parent 4eda352397
commit 3cff9d87e9
1 changed files with 20 additions and 1 deletions

@ -38,10 +38,29 @@ export default async function(verbose = false): Promise<string> {
const array = new Uint8Array(10)
const ratio = passwordSet.length / 255
self.crypto.getRandomValues(array)
getRandomValues(array)
let password = ''
for (let i = 0; i < array.length; i++) {
password += passwordSet.charAt(array[i] * ratio)
}
return password
}
/**
* Fills the given array with cryptographically secure random values.
* If the crypto API is not available, it falls back to less secure Math.random().
* Crypto API is available in modern browsers on secure contexts (HTTPS).
*
* @param {Uint8Array} array - The array to fill with random values.
*/
function getRandomValues(array: Uint8Array): void {
if (self?.crypto?.getRandomValues) {
self.crypto.getRandomValues(array)
return
}
let len = array.length
while (len--) {
array[len] = Math.floor(Math.random() * 256)
}
}