반응형
구글 OTP java 구현코드입니다.
[테스트 실행코드]
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class GoogleOtpUtilTest {
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
// user와 host는 GoogleOTP에서 다른 암호와 구분하기 위한 값으로 아무 값이나 넣어도 상관없다.
// user@host 형식으로 보이게 된다.
String user = "goni";
String host = "tistory";
String secret = "qkqhdidurlsdkanrkqtdlskTjehehl";
String qrBarcodeURL = GoogleOtpUtil.getQRBarcodeURL(user, host, secret);
// GoogleOTP 앱에 등록할 QRCode
System.out.println(qrBarcodeURL);
// GoogleOTP에 나온 6자리 숫자
String code = "245261";
boolean checkCode = GoogleOtpUtil.checkCode(secret, Long.parseLong(code));
System.out.println(checkCode);
}
}
[소스코드]
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base32;
public class GoogleOtpUtil {
public static String getQRBarcodeURL(String user, String host, String secret) {
String format = "https://www.google.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";
return String.format(format, user, host, secret);
}
public static boolean checkCode(String secret, long code) throws NoSuchAlgorithmException, InvalidKeyException {
Base32 codec = new Base32();
byte[] decodedKey = codec.decode(secret);
long t = new Date().getTime() / 30000;
// Window is used to check codes generated in the near past.
// You can use this value to tune how far you're willing to go.
int window = 3;
for (int i = -window; i <= window; ++i) {
long hash = verify_code(decodedKey, t + i);
if (hash == code) {
return true;
}
}
// The validation code is invalid.
return false;
}
private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {
byte[] data = new byte[8];
long value = t;
for (int i = 8; i-- > 0; value >>>= 8) {
data[i] = (byte) value;
}
SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
byte[] hash = mac.doFinal(data);
int offset = hash[20 - 1] & 0xF;
// We're using a long because Java hasn't got unsigned int.
long truncatedHash = 0;
for (int i = 0; i < 4; ++i) {
truncatedHash <<= 8;
// We are dealing with signed bytes:
// we just keep the first byte.
truncatedHash |= (hash[offset + i] & 0xFF);
}
truncatedHash &= 0x7FFFFFFF;
truncatedHash %= 1000000;
return (int) truncatedHash;
}
}
[Dependency] Base32
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
반응형
'java' 카테고리의 다른 글
mybatis jndi example (0) | 2018.11.05 |
---|---|
mybatis jdbcTypeForNull NULL (0) | 2018.11.05 |
java, springboot, hikari example (0) | 2018.10.30 |
ThreadLocal Test (0) | 2018.10.30 |
spring boot logging.config (0) | 2018.09.10 |
restTemplate large file download stream (0) | 2018.09.03 |
java - pretty log - 예쁜 로그를 남기자! (0) | 2018.08.30 |
java php aes ecb nopadding (2) | 2018.08.29 |