• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

generate 7 digit secure random number using securerandom

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to generate a 7 digit secure random number combination using javas secureRandom function

I found this code:



The problem with this one is that the length of the numbers is random between 6 or 7 digits, and it includes negative numbers. Is there any way for me to only generate positive digit and limit it to 7 digits, also is this way of using secureRandom safe or could it use some enhancement?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use nextInt(int bound) method for that.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:You can use nextInt(int bound) method for that.



but the nextInt will only create a range between 0 and the number you specified.

I made this code and it worked for me, I needed to do some manipulating of the length of the number though to get what I wanted.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean you want a random number between 1,000,000 and 9,999,999 (inclusive)?

Then just use nextInt() to generate a random number between 0 and 9,000,000 and add 1,000,000.

No while loop necessary.
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm curious about your use-case. Why do you need a cryptographically secure number like this?
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and what makes you think a 7‑digit number is cryptographically secure?
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in this context, cryptographically secure means that after seeing a sequence of these numbers, an attacker won't be able to predict the next 7 digit number. Using SecureRandom in this way is correct.

Whether the 7 digit number itself is being used correctly depends on the application, but is a security concern and is not related to cryptography.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Do you mean you want a random number between 1,000,000 and 9,999,999 (inclusive)?

Then just use nextInt() to generate a random number between 0 and 9,000,000 and add 1,000,000.

No while loop necessary.



sorry for the late reply, but Oh God! you can do that? Thank you for this I'll change my code right away

Stephan van Hulst wrote:I'm curious about your use-case. Why do you need a cryptographically secure number like this?



I am generating an OTP thus it needs to be secure and random

Campbell Ritchie wrote:…and what makes you think a 7‑digit number is cryptographically secure?



I did some reading and found out that a human brain can retain 7 +- 2 characters, and this OTP needs to be memorized so I chose to stick with 7, as 8 would be too long as well, also 7 digits have a 10,000,000 possible combination.
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That actually looks like a valid scenario! I'm content :P
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rodolfo tuble wrote:7 digits have a 10,000,000 possible combination.


Not if you disallow 0 as the first digit.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll only lose 10%, so there's still 9 million possible values.
 
There's a city wid manhunt for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic