rendezvous/www/lib/random-string.js

39 lines
851 B
JavaScript
Raw Permalink Normal View History

2023-06-21 15:33:47 +00:00
function randomString(n,s){
//
// review..
try{
n = n || 16
let b = new Uint8Array(n+1) // +1 for extra random value for initial start position
crypto.getRandomValues(b)
s = s || 'ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz01234567890' //-_'
let aa = []
let ii = 0
let len = s.length
// note: this might be biased? or might be bad to add seq random numbers?
for(let i = 0;i< b.length;i++){ // first value (index 0) to start in a random location.
let v = b[i]
ii += v
if(i > 0){
let iii = ii%len
let vv = s[iii]
if(!vv)
conole.log('out of range?',iii)
aa.push(vv)
}
}//for
let r = aa.join('')
return r
}
catch(e){
//
console.log('crypto not supported?')
return Math.random().toString(36).substring(7) // till crypto bug fix?
}
}
export default randomString