To Generate a One Time Password or Unique Identification URL
A one-time password (OTP) is a password that is valid for only one login session or transaction, on a computer system or other digital device. For more details refer this. Algorithm Randomly pick characters from our all possibilities and generate a string of the desired length from it. OTPs are generally 6-7 characters long and randomness in 6-7 characters almost guarantees a secure way of logging in. Applications
- OTPs are widely used in websites like- Facebook, Google Sign-in, Wifi – accessing, Railways Portal Login etc.
- Even the GeeksforGeeks IDE has a unique string for all the codes compiled through it. For example, https://ide.geeksforgeeks.org/Ks84Ck has the unique string – “Ks84Ck” at the end which is unique for this code only.
How it gets generated ? Well it is a great possibility that they uses the same algorithm as an OTP is generated. If by chance (very rare) the unique string generated is already been generated before and has been associated with a different code then another random string is used. As per now it seems that only six character strings are generated randomly for a unique identification of all codes. A time will come when all the possible six character strings may get exhausted. So yes, even the web-related stuffs also heavily relies on randomness. Probability of collision of two OTPs
- The length of OTP is 6 and the set size of all possible characters in the OTP is 62. So the total number of possible sets of the pair of OTPs are 6212.
- Some of them are – [{aaaaaa, aaaaaa}, {aaaaaa, aaaaab},…..{456789, 456788}, {456789, 456789}]
- But the possible sets of equal pair of OTPs are:626. Some of them are – [{aaaaaa, aaaaaa}, {aaaaab, aaaaab},…..{456788, 456788}, {456789, 456789}]
- Hence the probability of collision of two OTPs is: 626 / 6212 = 1 / 626 = 1 / 56800235584 = 1.7605561-11
So the probability of two OTPs colliding are as less probable as the existence of your life on earth (Ratio of the number of years you will live to the number of years from the start of the universe and everything in existence).So yes,OTPs are way more secure than static passwords ! Implementation
CPP
// A C/C++ Program to generate OTP (One Time Password) #include<bits/stdc++.h> 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 |
Python3
# 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 ( 1 ,length + 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 |
Output (May be different for every run):
Your OTP is - 8qOtzy
Time Complexity: O(N), where N = number of characters in our OTP Auxiliary Space: Apart from the string having all possible characters we require O(N) space to hold the OTP, where N = number of characters in our OTP This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...