Java Salt Value Create generateSaltOfLength(int length)

Here you can find the source of generateSaltOfLength(int length)

Description

Generates a salt (random alphanumeric string) of given length.

License

Open Source License

Parameter

Parameter Description
length of salt to be be generated

Return

generated salt

Declaration

public static String generateSaltOfLength(int length) 

Method Source Code


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

import java.util.Random;

public class Main {
    private static final String UPPERCASE_ALPHANUMERIC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    /**// ww w. jav a 2  s.c o m
     * Generates a salt (random alphanumeric string) of given length.
     * @param length of salt to be be generated
     * @return generated salt
     */
    public static String generateSaltOfLength(int length) {
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < length) {
            int index = rnd.nextInt(UPPERCASE_ALPHANUMERIC_CHARS.length());
            salt.append(UPPERCASE_ALPHANUMERIC_CHARS.charAt(index));
        }
        return salt.toString();
    }
}

Related

  1. generateSalt(int numberOfBytes)
  2. generateSalt(int numBytes)
  3. generateSaltAESPBKDF2()
  4. generateSaltedPassword(final String password, byte[] salt, int iterationsCount, String hmacName)
  5. generateSaltedSHAHash(String algorithm, String input, String salt)
  6. getMd5Salt()
  7. getNewSalt()
  8. getSalt()
  9. getSaltDES(int snmpEngineBoots)