admin管理员组文章数量:1356236
I want to generate a unique number everytime. I have used crypto module for that.
const alphanu = crypto.randomBytes(16).toString('hex')
This will generate alphanumeric string of length 32. I want to generate a number of length 8.
I tried randomInt as well.
const num = crypto.randomInt(10000000,99999999)
Will it always generate a unique number?
How do I achieve what I want?
I want to generate a unique number everytime. I have used crypto module for that.
const alphanu = crypto.randomBytes(16).toString('hex')
This will generate alphanumeric string of length 32. I want to generate a number of length 8.
I tried randomInt as well.
const num = crypto.randomInt(10000000,99999999)
Will it always generate a unique number?
How do I achieve what I want?
Share Improve this question edited Apr 16, 2023 at 13:38 Renee asked Apr 16, 2023 at 13:28 ReneeRenee 3218 silver badges26 bronze badges 8- You can't randomly create unique numbers without storing which numbers are already created. Why can't you simply start with 0 and increment? You could add a timestamp or a counter. – Thomas Sablik Commented Apr 16, 2023 at 13:32
- "Will it always generate a unique number?" No – Thomas Sablik Commented Apr 16, 2023 at 13:36
- @ThomasSablik I wanted to bypass some checks for certain users, that's the reason why I was creating 8 digit number. – Renee Commented Apr 16, 2023 at 13:38
- 1 Start with 10000000 and increment. Why do you need a random number? – Thomas Sablik Commented Apr 16, 2023 at 13:38
- @ThomasSablik if I stop and run the program, there is no way the incremented number is remembered. I would have to make a database call to get to know the number and this makes it plicated but if I dont find any other solution, then I will follow this method. – Renee Commented Apr 16, 2023 at 13:41
3 Answers
Reset to default 5Your "unique" requirement will be harder to achieve than you think. If you meant "non-deterministic", then just use crypto.randomInt()
as you did in your question:
crypto.randomInt(10**7, 10**8-1) // 8 digit, no leading zeroes
crypto.randomInt(0, 10**8-1).toString().padStart(8, "0") // 8 digits, allows leading zeroes
Technically speaking, this is psuedorandom, not random. However, for most use cases, you won't be able to tell the difference.
Now if you need unique, then here's two fairly easy approaches you could use:
- Store every number you've already used in a database, or
- Start at 0 and increment by one each time and add leading zeroes if necessary (or start at 10^7 if you don't want leading zeroes). With this, all you need to do is store the last number used. However, with this approach, the result is deterministic, which might be a security drawback depending on your use case.
To make a random and unique number, you're going to have to mix .random()
and a time stamp together.
Here's a simple UID generator I've been using for a while now, I've modified the .substring()
so that it returns 8 characters.
const alphanu = Date.now().toString(36) + Math.random().toString(36).substring(13);
console.log(alphanu);
For guaranteed unique numbers you are right to use encryption. Because encryption is one-to-one then unique inputs guarantee unique outputs. Just encrypt 0, 1, 2, 3, ... with the same key (and IV if used) and you will get unique numbers out.
You want 8 digit numbers, so the output range is 0 .. 99,999,999. That is a 27 bit number. You could use a 32 bit encryption (which gives 10 digits) with cycle walking to get numbers within range. If the cycle walking takes too long, then you could either accept a 10 digit output or write a simple 28-bit Feistel cipher with four or six rounds. That will not be cryptographically secure, but will give you enough pseudo-randomness with guaranteed uniqueness.
本文标签: javascriptHow to generate a unique 8 digit number using crypto in node jsStack Overflow
版权声明:本文标题:javascript - How to generate a unique 8 digit number using crypto in node js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744039829a2580457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论