issue # 랜덤 코드 생성기 구현
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.baekyangdan.scheduler.utils.generator;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class RandomCodeGenerator {
|
||||
private static final String DIGITS = "0123456789";
|
||||
private static final SecureRandom random = new SecureRandom();
|
||||
|
||||
public static String generateRandomCode(int length) {
|
||||
List<Character> codeCharacters = ArrayList<>(length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
codeCharacters.add(randomChar());
|
||||
}
|
||||
|
||||
Collections.shuffle(codeCharacters, random);
|
||||
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
|
||||
for (char c : codeCharacters) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static char randomChar() {
|
||||
int idx = random.nextInt(DIGITS.length());
|
||||
return DIGITS.charAt(idx);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user