Java Salt Value Create generateSalt(final Integer size)

Here you can find the source of generateSalt(final Integer size)

Description

http://stackoverflow.com/a/18268562/2648956

License

Open Source License

Parameter

Parameter Description
size the size of the desired salt

Return

the salt

Declaration

public static final String generateSalt(final Integer size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.SecureRandom;
import java.util.Random;

public class Main {
    /**//from   w  ww.j  av  a  2 s.  c o m
     * http://stackoverflow.com/a/18268562/2648956
     *
     * @param size the size of the desired salt
     * @return the salt
     */
    public static final String generateSalt(final Integer size) {
        final Random r = new SecureRandom();
        byte[] salt = new byte[size / 2];
        r.nextBytes(salt);
        return bytesToString(salt);
    }

    private static final String bytesToString(byte[] bytes) {
        StringBuffer encryptedUserID = new StringBuffer();
        for (int i = 0; i < bytes.length; ++i) {
            String hex = Integer.toHexString(0xff & bytes[i]);
            if (hex.length() == 1) {
                encryptedUserID.append('0');
            }
            encryptedUserID.append(hex);
        }

        return encryptedUserID.toString();
    }
}

Related

  1. generateSalt()
  2. generateSalt()
  3. generateSalt()
  4. generateSalt()
  5. generateSalt()
  6. generateSalt(int bits)
  7. generateSalt(int byteSize)
  8. generateSalt(int length)
  9. generateSalt(int numberOfBytes)