Here you can find the source of generateHashSalt(int length)
Parameter | Description |
---|---|
length | length of salt string to generate |
public static String generateHashSalt(int length)
//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(); } }