I need to create cryptographically secure PIN number in Java using secret id for each user.

This simple approach comes to mind:

int getPin(String secretUserIdStr)
    String pepper = "randomPepper...";
    String seed = secretUserIdStr + pepper;

    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");  

    return sr.nextInt(10000);

Is this good idea? Is it secure? If not, what is the best practice?

  • Why does the PIN have to be based on the User-ID? What's to stop you from just generating one at random? What's the advantage? Dec 10, 2014 at 15:13
  • Reason is that PIN is not stored in the database, so I need to be able to recreate it. In the context of this question, user id is to be considered secure (secret). Question is regarding the usage of explicitly seeded SecureRandom as a mean of creating "secure hash", which is what I'm doing here actually.
    – h9lpq0u
    Dec 10, 2014 at 15:16

1 Answer 1


Don't do it. Reason: general principles. ("Don't roll your own crypto.")

You're deriving a key (something secret) from something else secret. You don't need to invent a procedure for yourself. Go with an established key derivation function. Something like PBKDF2.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .