What's the best way to produce a SALT value in Java as a String that's at least 32 bytes long?
Asked
Active
Viewed 2.6k times
29
-
1A salt value is just a randomly produced values. What range of characters do you want? Note: characters uses 2-bytes. Do you mean you want a `byte[32]` as a salt? – Peter Lawrey Aug 16 '13 at 07:58
-
2I wanted 32 random bytes encoded as a String as Shamim suggests. – Tom Bell Aug 16 '13 at 15:46
2 Answers
44
final Random r = new SecureRandom();
byte[] salt = new byte[32];
r.nextBytes(salt);
/** String encodedSalt = Base64.encodeBase64String(salt); */

Shamim Ahmmed
- 8,265
- 6
- 25
- 36
3
In SpringSecurity you can use org.springframework.security.crypto.keygen.KeyGenerators

Michael
- 10,063
- 18
- 65
- 104