标准JAVA代码 MD5方法随机字母大小写拼接数字

/*
 *JAVA代码 MD5方法随机字母大小写拼接数字 
 **/
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import org.springframework.util.DigestUtils;
public class TestStore {
	public static void main(String[] args) throws NoSuchAlgorithmException {
		Set<String> set = new HashSet<String>();
		Long startTime = new Date().getTime();
		while (true) {
			String code = getCharAndNumr(10);
			set.add(code);
			if (set.size() == 1) {
				break;
			}
		}
		Long endTime = new Date().getTime();
		long time = (endTime - startTime);
		System.out.println("生成个数:" + set.size());
		System.out.println("生成耗时" + time + "毫秒,约" + (time / 1000) + "秒");//
		for (String string : set) {

			String salt = "" + string.toString(); // 盐值
			System.out.println("salt=" + salt);
			System.out.println(salt.length());
			String str = "123456";
			String result = DigestUtils.md5DigestAsHex(str.getBytes()).toUpperCase();
			System.out.println("原密码加密:" + result);
			String finalResult = DigestUtils.md5DigestAsHex((result + salt).getBytes());
			System.out.println(finalResult.toUpperCase());
		}
	}

	/**
	 * java生成随机数字和字母组合
	 * 
	 * @param length[生成随机数的长度]
	 * 
	 */
	public static String getCharAndNumr(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			// 输出字母还是数字
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 字符串
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 取得大写字母还是小写字母
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}
}

生成个数:1
生成耗时0毫秒,约0秒
salt=kX568i01SY
10
原密码加密:E10ADC3949BA59ABBE56E057F20F883E
47326D2A601569E4E094F551E1D1F644

猜你喜欢

转载自blog.csdn.net/weixin_43341426/article/details/83590433