SecureRandom in JavaScript?

Is there a SecureRandom.hex()-like (ruby) function in JavaScript that generates a random hash for me?

+5
source share
1 answer

There is no such helper function in JS. You can generate a fairly random hash using:

function hex(n){
 n = n || 16;
 var result = '';
 while (n--){
  result += Math.floor(Math.random()*16).toString(16).toUpperCase();
 }
 return result;
}

You can modify it to form a guid:

function generateGuid(){
 var result = '', n=0;
 while (n<32){
  result += (~[8,12,16,20].indexOf(n++) ? '-': '') +    
            Math.floor(Math.random()*16).toString(16).toUpperCase();
 }
 return result;
}
+3
source

All Articles