Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate SALT value in Java?

What's the best way to produce a SALT value in Java as a String that's at least 32 bytes long?

like image 931
Tom Bell Avatar asked Aug 16 '13 07:08

Tom Bell


People also ask

How do you generate a random salt in Java?

To generate salt bytes in Java, we just need to make sure that we use a secure random number generator. Construct an instance of SecureRandom, create (say) a 20-byte array, and call nextBytes() on that array: Random r = new SecureRandom(); byte[] salt = new byte[20]; r. nextBytes(salt);

How are salt values generated?

According to OWASP Guidelines, a salt is a value generated by a cryptographically secure function that is added to the input of hash functions to create unique hashes for every input, regardless of the input not being unique.

How does SHA256 with salt work?

A salt is a random character string that is added to the beginning or the end of a password. This salt is unique to each user, and is stored in the database along with the username and salted-hashed password. An example username-password database using the SHA256 hashing function with a salt.

What is salt in SHA512?

The Salted SHA512 Password Storage Scheme provides a mechanism for encoding user passwords using a salted form of the 512-bit SHA-2 message digest algorithm.


2 Answers

final Random r = new SecureRandom(); byte[] salt = new byte[32]; r.nextBytes(salt); /** String encodedSalt = Base64.encodeBase64String(salt); */ 
like image 188
Shamim Ahmmed Avatar answered Sep 30 '22 06:09

Shamim Ahmmed


In SpringSecurity you can use org.springframework.security.crypto.keygen.KeyGenerators

http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/org/springframework/security/crypto/keygen/KeyGenerators.html

http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#spring-security-crypto-keygenerators

like image 29
Michael Avatar answered Sep 30 '22 07:09

Michael