-1
I need to generate a random alphanumeric string that needs to be 32 bytes. Is there any direct function to do this?
-1
I need to generate a random alphanumeric string that needs to be 32 bytes. Is there any direct function to do this?
1
Using Crypto and a typed array
function random32bit() {
let u = new Uint32Array(1);
window.crypto.getRandomValues(u);
let str = u[0].toString(16).toUpperCase();
return '00000000'.slice(str.length) + str;
}
0
Here has a very good discussion about UUID
And here is an example you can find on the first link:
function uuid() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
console.log(uuid());
Using Math,
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
console.log(uuid())
"Crypto.getRandomValues" is not working on Ionic...
Don’t have to import Crypto on Ionic? Showing which error?
https://answall.com/questions/373839/como-usar-requirecrypto-no-ionic-4
https://www.npmjs.com/package/crypto
I added with Math in the reply
Managed to solve the problem?
Browser other questions tagged javascript ionic
You are not signed in. Login or sign up in order to post.
". toString(16)" is used to turn the number into the right hexadecimal? or am I wrong?
– user144364