본문 바로가기
BoB/BoB생활일지

OTP(One-Time-Password) 개발

by reindeer002 2022. 3. 5.
728x90

간단하게 OTP를 개발한 내역을 아래 코드로 기록해두었다.

OTP에 대한 자세한 내용은 아래 나무위키를 확인해보면 된다.

https://namu.wiki/w/OTP


<?php
	$rest = time() % 60;
	$key = time() - $rest;
	$salt = "BoBOTP";
	echo md5($key . $salt) . "\n"; 
	$answer = substr(md5($key . $salt), -3, 3);
	if (!isset($_SERVER['REMOTE_ADDR'])) echo $answer . "\n";
	else if ($_GET['key'] == $answer) echo "Success";
?>

 

#include <stdio.h>
#include <openssl/md5.h>
#include <time.h>
#include <string.h>

void MD5Hash(char *string)
{
	int i;
	unsigned char digest[MD5_DIGEST_LENGTH];
	char md5Hash[50] = {0,};
	char *message;
	message = strcat(string, "BoBOTP");

	MD5_CTX context;
	MD5_Init(&context);
	MD5_Update(&context, message, strlen(string));
	MD5_Final(digest, &context);

	for(i = 0;i<MD5_DIGEST_LENGTH; ++i)
		sprintf(md5Hash+(i*2), "%02x", digest[i]);

	printf("Result of md5(%s) : %s\n", string, md5Hash);
}

int main()
{
	char str_time_key[50];
	unsigned long unix_time = time(NULL);
	printf("%lu\n", unix_time);
	unsigned long rest = unix_time % 60;
	unsigned long time_key = unix_time - rest;
	printf("%lu\n", time_key);
	sprintf(str_time_key, "%lu", time_key);

	MD5Hash(str_time_key);
	int cnt_time = 60 - rest;
	do
	{
		if(cnt_time > 0) 
		{
			sleep(1);
			cnt_time--;
			printf("%d.", cnt_time);
			fflush(stdout);
			continue;
		}
		unix_time = time(NULL);
		rest = unix_time % 60;
		time_key = unix_time - rest;
		sprintf(str_time_key, "%lu", time_key);
		printf("\n");
		MD5Hash(str_time_key);
		cnt_time = 60;
	}while(1);
}
728x90

'BoB > BoB생활일지' 카테고리의 다른 글

BOB 10기 합격 후기  (0) 2021.07.14

댓글