How to generate a secure random alphanumeric string in Java efficiently?

后端 未结 9 1432
半阙折子戏
半阙折子戏 2021-01-01 12:24

How do you generate a secure random (or pseudo-random) alphanumeric string in Java efficiently?

9条回答
  •  生来不讨喜
    2021-01-01 12:49

    Initialize an array containing all the accepted chars (CHARS_ARRAY), then instantiate a SecureRandom instance, and call nextInt(CHARS_ARRAY.length) repeatedly to get a random index in your char array. Append each char to a StringBuilder until you get the expected number of chars.

提交回复
热议问题