| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.utils; |
| | | |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | |
| | | import java.security.SecureRandom; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Created by IntelliJ IDEA. |
| | | * |
| | | * @Author : Rk |
| | | * @create 2025/10/27 16:23 |
| | | */ |
| | | public class PasswordGenerator { |
| | | |
| | | private static final String LOWER = "abcdefghijklmnopqrstuvwxyz"; |
| | | private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| | | private static final String DIGITS = "0123456789"; |
| | | private static final String SYMBOLS = "!@#$%^&*()_+-=[]{};':|,.<>/?"; |
| | | private static final String PASSWORD_CHARS = LOWER + UPPER + DIGITS + SYMBOLS; |
| | | private static final int PASSWORD_LENGTH = 8; // ä½ å¯ä»¥æ ¹æ®éè¦è°æ´å¯ç é¿åº¦ |
| | | |
| | | public static void getPasswordTypeCount(String password){ |
| | | Set<Integer> typeCount = new HashSet<>(4); |
| | | for (int i = 0; i < password.length(); i++) { |
| | | String key = password.substring(i,i+1); |
| | | if(LOWER.indexOf(key)>=Constants.ZERO){ |
| | | typeCount.add(Constants.ZERO); |
| | | }else if(UPPER.indexOf(key)>=Constants.ZERO){ |
| | | typeCount.add(Constants.ONE); |
| | | }else if(DIGITS.indexOf(key)>=Constants.ZERO){ |
| | | typeCount.add(Constants.TWO); |
| | | }else if(SYMBOLS.indexOf(key)>=Constants.ZERO){ |
| | | typeCount.add(Constants.THREE); |
| | | }else{ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请使ç¨åæ³çå¯ç å
容"); |
| | | } |
| | | } |
| | | if(typeCount.size()<Constants.THREE||password.length()<8){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"å¯ç è³å°å
æ¬æ°åã大å忝ãå°å忝ãç¹æ®å符ä¸çä¸ç§å符ï¼ä¸è³å°8使°"); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public static String generatePassword() { |
| | | SecureRandom random = new SecureRandom(); |
| | | StringBuilder password = new StringBuilder(PASSWORD_LENGTH); |
| | | Set<Character> usedChars = new HashSet<>(); |
| | | |
| | | // while (password.length() < PASSWORD_LENGTH) { |
| | | // int index = random.nextInt(PASSWORD_CHARS.length()); |
| | | // char randomChar = PASSWORD_CHARS.charAt(index); |
| | | // // ç¡®ä¿æ¯ç§ç±»åçå符è³å°åºç°ä¸æ¬¡ |
| | | // if (usedChars.add(randomChar)) { |
| | | // password.append(randomChar); |
| | | // } |
| | | // } |
| | | // 妿éè¦ç¡®ä¿æ¯ç§ç±»åé½è³å°åºç°ä¸æ¬¡ï¼å¯ä»¥æ³¨éæä¸é¢ç循ç¯ï¼ä½¿ç¨ä¸é¢ç代ç ï¼ |
| | | for (int i = 0; i < PASSWORD_LENGTH; i++) { |
| | | int type = random.nextInt(4); // 0=lower, 1=upper, 2=digit, 3=symbol |
| | | String charSet; |
| | | switch (type) { |
| | | case 0: charSet = LOWER; break; |
| | | case 1: charSet = UPPER; break; |
| | | case 2: charSet = DIGITS; break; |
| | | default: charSet = SYMBOLS; break; |
| | | } |
| | | int index = random.nextInt(charSet.length()); |
| | | password.append(charSet.charAt(index)); |
| | | } |
| | | // Shuffle the string to avoid predictable patterns (optional) |
| | | password.toString().chars() |
| | | .mapToObj(c -> (char) c) |
| | | .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append) |
| | | .toString(); // This line is a no-op, but it's here to show the method call for completeness. |
| | | return password.toString(); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println("Generated Password: " + generatePassword()); |
| | | } |
| | | } |