0

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");  
    sr.setSeed(seed.getBytes());

    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? – StackzOfZtuff Dec 10 '14 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 '14 at 15:16
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.