標準JAVA程式碼 MD5方法隨機字母大小寫拼接數字
阿新 • • 發佈:2019-02-01
/* *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