logo

일회용 비밀번호 또는 고유 식별 URL을 생성하려면

일회용 비밀번호(OTP)는 컴퓨터 시스템이나 기타 디지털 장치에서 한 번의 로그인 세션이나 거래에만 유효한 비밀번호입니다. 자세한 내용은 참조하세요 이것 . 연산 모든 가능성 중에서 무작위로 문자를 선택하고 그 중에서 원하는 길이의 문자열을 생성합니다. OTP의 길이는 일반적으로 6~7자이며 6~7자의 임의성이 거의 안전한 로그인 방법을 보장합니다.

OTP는 Facebook Google 로그인 Wi-Fi – 철도 포털 로그인 액세스 등과 같은 웹사이트에서 널리 사용됩니다.

어떻게 생성되나요?

글쎄요, OTP가 생성될 때 동일한 알고리즘을 사용할 가능성이 큽니다. 우연히(매우 드물게) 생성된 고유 문자열이 이전에 이미 생성되어 다른 코드와 연결된 경우 다른 임의 문자열이 사용됩니다. 현재로서는 모든 코드를 고유하게 식별하기 위해 무작위로 6개의 문자열만 생성되는 것 같습니다. 6개의 문자열이 모두 소진될 때가 올 것입니다. 그렇습니다. 웹 관련 항목도 무작위성에 크게 의존합니다.

개연성 두 OTP의 충돌 

  • OTP의 길이는 6이고 OTP에서 가능한 모든 문자의 설정 크기는 62입니다. 따라서 OTP 쌍의 가능한 총 집합 수는 다음과 같습니다. 62 12 .
  • 그 중 일부는 - [{AAAAAAAAAA} {AAAAAAAAAAAB} ..... {456789 456788} {456789 456789}]
  • 그러나 가능한 동일한 OTP 쌍 세트는 다음과 같습니다. 62 6 . 그 중 일부는 - [{AAAAAAAA} {AAAAAB AAAAAAB} ..... {456788 456788} {456789 456789}]
  • 따라서 개연성 두 OTP의 충돌은 다음과 같습니다. 62 6 / 62 12 = 1/62 6 = 1 / 56800235584 = 1.7605561 -11

그래서 개연성 두 개의 OTP가 충돌할 가능성은 지구상에서 당신의 삶이 존재할 가능성만큼 낮습니다(당신이 살게 될 연수와 우주의 시작과 존재하는 모든 것으로부터의 연수 비율).그래서 OTP는 정적 비밀번호보다 훨씬 더 안전합니다! 구현  

CPP
// A C/C++ Program to generate OTP (One Time Password) #include   using namespace std; // A Function to generate a unique OTP everytime string generateOTP(int len) {  // All possible characters of my OTP  string str = 'abcdefghijklmnopqrstuvwxyzABCD'  'EFGHIJKLMNOPQRSTUVWXYZ0123456789';  int n = str.length();  // String to hold my OTP  string OTP;  for (int i=1; i<=len; i++)  OTP.push_back(str[rand() % n]);  return(OTP); } // Driver Program to test above functions int main() {  // For different values each time we run the code  srand(time(NULL));  // Declare the length of OTP  int len = 6;  printf('Your OTP is - %s' generateOTP(len).c_str());  return(0); } 
Java
// A Java Program to generate OTP (One Time Password) class GFG{ // A Function to generate a unique OTP everytime static String generateOTP(int len) {  // All possible characters of my OTP  String str = 'abcdefghijklmnopqrstuvwxyzABCD'  +'EFGHIJKLMNOPQRSTUVWXYZ0123456789';  int n = str.length();  // String to hold my OTP  String OTP='';  for (int i = 1; i <= len; i++)  OTP += (str.charAt((int) ((Math.random()*10) % n)));  return(OTP); } // Driver code public static void main(String[] args) {  // Declare the length of OTP  int len = 6;  System.out.printf('Your OTP is - %s' generateOTP(len)); } } // This code is contributed by PrinciRaj1992 
Python
# A Python3 Program to generate OTP (One Time Password) import random # A Function to generate a unique OTP everytime def generateOTP(length): # All possible characters of my OTP str = 'abcdefghijklmnopqrstuvwxyzAB  CDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; n = len(str); # String to hold my OTP OTP = ''; for i in range(1length+1): OTP += str[int(random.random()*10) % n]; return (OTP); # Driver code if __name__ == '__main__': # Declare the length of OTP length = 6; print('Your OTP is - ' generateOTP(length)); # This code contributed by Rajput-Ji 
C#
// A C# Program to generate OTP (One Time Password) using System; class GFG {  // A Function to generate a unique OTP everytime  static string generateOTP(int len)  {  // All possible characters of my OTP  string str = 'abcdefghijklmnopqrstuvwxyzABCD'  + 'EFGHIJKLMNOPQRSTUVWXYZ0123456789';  int n = str.Length;  // Creating a new Random object  Random rand = new Random();  // String to hold my OTP  string OTP = '';  for (int i = 1; i <= len; i++)  OTP += (str[((int)((rand.Next() * 10) % n))]);  return (OTP);  }  // Driver code  public static void Main(string[] args)  {  // Declare the length of OTP  int len = 6;  Console.WriteLine('Your OTP is - '  + generateOTP(len));  } } // This code is contributed by phasing17 
JavaScript
// JavaScript Program to generate OTP (One Time Password) // A Function to generate a unique OTP everytime function generateOTP(length) {  // All possible characters of my OTP  let str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';  let n = str.length;  // String to hold my OTP  let OTP = '';  for (var i = 1; i <= length; i++)  OTP += str[(Math.floor(Math.random() * 10) % n)];  return (OTP); } // Driver code // Declare the length of OTP let length = 6; console.log('Your OTP is - ' generateOTP(length)); // This code is contributed by phasing17 

출력(실행마다 다를 수 있음):

Your OTP is - 8qOtzy

시간 복잡도: O(N) 여기서 N = OTP의 문자 수 보조 공간: 가능한 모든 문자를 포함하는 문자열 외에도 OTP를 보관하려면 O(N) 공간이 필요합니다. 여기서 N은 OTP의 문자 수입니다. GeeksforGeeks를 좋아하고 기여하고 싶다면 다음을 사용하여 기사를 작성할 수도 있습니다. write.geeksforgeeks.org 또는 기사를 [email protected]로 우편으로 보내세요. GeeksforGeeks 메인 페이지에 나타나는 기사를 보고 다른 Geeks를 도와주세요. 잘못된 내용을 발견했거나 위에서 논의한 주제에 대해 더 많은 정보를 공유하고 싶다면 의견을 작성해 주세요.