WindChat/windchat-business/src/main/java/com/windchat/im/business/utils/StringRandomUtils.java

23 lines
718 B
Java
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.windchat.im.business.utils;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class StringRandomUtils {
private static final String STR_62_RANDOM = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 随机生成N位字符串A-Za-z0-9
public static String generateRandomString(int length) throws NoSuchAlgorithmException {
SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
StringBuffer newRandomStr = new StringBuffer();
for (int i = 0; i < length; ++i) {
int number = sRandom.nextInt(STR_62_RANDOM.length());
newRandomStr.append(STR_62_RANDOM.charAt(number));
}
return newRandomStr.toString();
}
}