Skip to content

Instantly share code, notes, and snippets.

@pjc0247
Last active December 16, 2015 13:38
Show Gist options
  • Save pjc0247/5442579 to your computer and use it in GitHub Desktop.
Save pjc0247/5442579 to your computer and use it in GitHub Desktop.
Simple One-Time-Password implementation
# 현재 유닉스 타임을 얻어온다
def getCurrentUnixTime
Time.new.to_i
end
# 해당 유닉스 타임에 대한 타임 토큰을 계산한다
def getTimeToken(unix_time, token_size)
unix_time / token_size
end
# 타임 토큰을 기반으로 OTP를 생성한다
def generateOTP(time_token, private_key)
srand time_token * private_key
rand(90000) + 10000
end
# 테스트 코드
begin
TOKEN_SIZE = 30
PRIVATE_KEY = 4321
# OTP 발급받기
otp = generateOTP(
getTimeToken(
getCurrentUnixTime, TOKEN_SIZE
),
PRIVATE_KEY
)
puts otp
# OTP 확인하기
time_token = getTimeToken( getCurrentUnixTime, TOKEN_SIZE)
if generateOTP( time_token, PRIVATE_KEY ) == otp or
generateOTP( time_token-1, PRIVATE_KEY ) == otp
puts "OTP 인증 성공"
else
puts "실패"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment