Java Hash Code Calculate generateHashSalt(int length)

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

Description

Generates a string of random alphanumeric characters of the given length.

License

Open Source License

Parameter

Parameter Description
length length of salt string to generate

Return

salt string

Declaration

public static String generateHashSalt(int length) 

Method Source Code


//package com.java2s;
/*//from  w w w.  j  a v  a2s  . c om
 BillingNG, a next-generation billing solution
 Copyright (C) 2010 Brian Cowdery
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html
 */

import java.security.SecureRandom;

public class Main {
    private static final char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
            .toCharArray();

    /**
     * Generates a string of random alphanumeric characters of the given length. The generated salt can be
     * used to add entropy to a hashing algorithm or token generator, thus making the result harder to crack.
     *
     * @param length length of salt string to generate
     * @return salt string
     */
    public static String generateHashSalt(int length) {
        SecureRandom random = new SecureRandom();

        StringBuffer salt = new StringBuffer(length);
        for (int i = 0; i < length; i++)
            salt.append(chars[random.nextInt(chars.length)]);

        return salt.toString();
    }
}

Related

  1. generateHash(String plaintext)
  2. generateHash(String plainText, String hashType)
  3. generateHash(String serviceUri, String username, String password)
  4. generateHash(String target)
  5. generateHash(String tcString)
  6. generateHashUUID(String digestData)
  7. hashCode(boolean b)
  8. hashCode(boolean b)
  9. hashCode(boolean bool)