Java Salt Value Create generateSalt(int numberOfBytes)

Here you can find the source of generateSalt(int numberOfBytes)

Description

md5 128bit 16bytes sha1 160bit 20bytes sha256 256bit 32bytes sha384 384bit 48bites sha512 512bit 64bites

License

Open Source License

Declaration

public static String generateSalt(int numberOfBytes) 

Method Source Code

//package com.java2s;

public class Main {
    private static final java.security.SecureRandom random = new java.security.SecureRandom();
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**//from w ww .jav a  2s . c o  m
     * md5 128bit 16bytes
     * sha1 160bit 20bytes
     * sha256 256bit 32bytes
     * sha384 384bit 48bites
     * sha512 512bit 64bites
     */
    public static String generateSalt(int numberOfBytes) {
        byte[] salt = new byte[numberOfBytes];
        random.nextBytes(salt);
        return toHex(salt);
    }

    private static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }
}

Related

  1. generateSalt()
  2. generateSalt(final Integer size)
  3. generateSalt(int bits)
  4. generateSalt(int byteSize)
  5. generateSalt(int length)
  6. generateSalt(int numBytes)
  7. generateSaltAESPBKDF2()
  8. generateSaltedPassword(final String password, byte[] salt, int iterationsCount, String hmacName)
  9. generateSaltedSHAHash(String algorithm, String input, String salt)