본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.



자바 랜덤함수를 이용한 인증번호 인증문자 만들기


회원가입에 필요한 숫자 OR 문자 인증번호와 임시 비밀번호를 생성하여 제공하기 위해 간단한 함수를 만들어 보았습니다. 만든 기능은 총 3가지입니다.


1. 숫자 인증번호 생성

2. 문자+숫자 인증번호 생성

3. 임시 비밀번호(문자+숫자+특수문자 포함) 생성


전반적으로 임의의 랜덤한 값을 생성하기 위해 Random Class를 사용하였고, 중복 방지를 위해 생성자에 System.currentTimeMillis()를 넣었습니다.


1. 숫자 인증번호 생성

기본적으로 숫자 인증 번호를 6자리로 생성하였고, setter을 통해서 자리수 변경이 가능합니다.

인증번호 자리수를 제곱하여 range를 구하고 자리수의 -1을 하여 10을 다시 제곱하여 trim값으로 설정하였습니다. 

import java.util.Random;

public class GenerateCertNumber{
    private int certNumLength = 6;
    
    public String excuteGenerate() {
        Random random = new Random(System.currentTimeMillis());
        
        int range = (int)Math.pow(10,certNumLength);
        int trim = (int)Math.pow(10, certNumLength-1);
        int result = random.nextInt(range)+trim;
         
        if(result>range){
            result = result - trim;
        }
        
        return String.valueOf(result);
    }

    public int getCertNumLength() {
        return certNumLength;
    }

    public void setCertNumLength(int certNumLength) {
        this.certNumLength = certNumLength;
    }
    
    public static void main(String[] args) {
        GenerateCertNumber ge = new GenerateCertNumber();
        ge.setCertNumLength(5);
        System.out.println(ge.excuteGenerate());
    }
}


2. 문자+숫자 인증번호 생성

기본적으로 문자+숫자 인증번호를 8자리로 생성하였고, setter을 통해서 자리수 변경이 가능합니다.

문자+숫자 인증번호를 생성하기 위핸 Table을 만들고, 테이블의 길이를 구하여 Random.nextInt에 넣었습니다.



import java.util.Random;

public class GenerateCertCharacter{
    private int certCharLength = 8;
    private final char[] characterTable = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 
                                            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
                                            'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
    
    public String excuteGenerate() {
        Random random = new Random(System.currentTimeMillis());
        int tablelength = characterTable.length;
        StringBuffer buf = new StringBuffer();
        
        for(int i = 0; i < certCharLength; i++) {
            buf.append(characterTable[random.nextInt(tablelength)]);
        }
        
        return buf.toString();
    }

    public int getCertCharLength() {
        return certCharLength;
    }

    public void setCertCharLength(int certCharLength) {
        this.certCharLength = certCharLength;
    }
}


3. 임시 비밀번호(문자+숫자+특수문자 포함) 생성

기본적으로 문자+숫자 인증번호를 8자리로 생성하였고, setter을 통해서 자리수 변경이 가능합니다.

문자+숫자 인증번호 생성에서 사용한 테이블에 임시 비밀번호에 사용할 특수문자를 추가하였습니다.

참고로 Kisa에서 제공하는 시큐어 코딩 가이드에서 보안을 위해 문자+숫자+특수문자를 사용할 것을 권장하였습니다. 그래서 3가지의 문자를 섞었습니다.

import java.util.Random;

public class GenerateCertPassword{
    private int pwdLength = 8;
    private final char[] passwordTable =  { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 
                                            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                                            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
                                            'w', 'x', 'y', 'z', '!', '@', '#', '$', '%', '^', '&', '*',
                                            '(', ')', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

    public String excuteGenerate() {
        Random random = new Random(System.currentTimeMillis());
        int tablelength = passwordTable.length;
        StringBuffer buf = new StringBuffer();
        
        for(int i = 0; i < pwdLength; i++) {
            buf.append(passwordTable[random.nextInt(tablelength)]);
        }
        
        return buf.toString();
    }

    public int getPwdLength() {
        return pwdLength;
    }

    public void setPwdLength(int pwdLength) {
        this.pwdLength = pwdLength;
    }
}