Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate unique random string with Javascript

I am creating an app where I sometimes need to allow user to generate some random strings. I would like to force that to be generated in the following format:

xxxx-xxxx-xxxx

Where "x" is some number [0-9] or character [A-Z]. What would the most efficient way to do this? When generated, I would also need to check does it already exist in database so I am a little bit worried about the time which it would take.

like image 723
user4386126 Avatar asked Dec 20 '15 23:12

user4386126


1 Answers

We can make it so simple like

require("crypto").randomBytes(64).toString('hex')
like image 77
yasarui Avatar answered Oct 04 '22 00:10

yasarui