Kertaluonteinen salasana (OTP) on salasana, joka on voimassa vain yhdelle kirjautumisistunnolle tai tapahtumalle tietokonejärjestelmässä tai muussa digitaalisessa laitteessa. Lisätietoja on tämä . Algoritmi Valitse satunnaisesti merkkejä kaikista mahdollisuuksistamme ja luo siitä haluttu pituus. OTP: t ovat yleensä 6-7 merkkiä pitkiä ja satunnaisuus 6-7 merkissä takaavat melkein turvallisen tavan kirjautua sisään.
OTP: tä käytetään laajasti verkkosivustoilla, kuten Facebook Google -kirjautuminen WiFi- Railways Portaalin kirjautumisen käyttö jne.
Kuinka se syntyy?
No, on suuri mahdollisuus, että he käyttävät samaa algoritmia kuin OTP syntyy. Jos sattumalta (erittäin harvinainen) luotu ainutlaatuinen merkkijono on jo luotu aiemmin ja siihen on liitetty erilainen koodi, käytetään toista satunnaista merkkijonoa. Nyt näyttää siltä, että vain kuusi merkkijonoa luodaan satunnaisesti kaikkien koodien yksilöllisen tunnistamiseksi. Aika tulee, jolloin kaikki mahdolliset kuusi merkkijonoa saattavat loppuun. Joten kyllä, jopa verkkoon liittyvät tavarat luottavat myös voimakkaasti satunnaisuuteen.
Todennäköisyys kahden OTP: n törmäys
- OTP: n pituus on 6 ja kaikkien mahdollisten merkkien asetettu koko OTP: ssä on 62. Joten OTPS -parin mahdollisten joukkojen kokonaismäärä on 62 12 .
- Jotkut heistä ovat - [{aaaaaaaaa} {aaaaaaaaaaab} ..... {456789 456788} {456789 456789}]
- Mutta mahdolliset sarjat, joissa on yhtä suurta OTPS -paria, ovat: 62 6 . Jotkut heistä ovat - [{AAAAAAAAAA} {AAAAAB AAAAAAB} ..... {456788 456788} {456789 456789}]
- Siksi todennäköisyys Kahden OTP: n törmäys on: 62 6 / 62 12 = 1/62 6 = 1/56800235584 = 1,7605561 -11
Niin todennäköisyys Kahdesta OTP: stä törmäävät ovat yhtä todennäköisiä kuin elämäsi olemassaolo maapallolla (suhde vuosien lukumäärään, jolloin elät vuosien lukumäärään maailmankaikkeuden alusta ja kaikki olemassaolo) .Käytökset ovat paljon turvallisempia kuin staattiset salasanat! Toteutus
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
Lähtö (voi olla erilainen jokaiselle juoksemiselle):
Your OTP is - 8qOtzy
Ajan monimutkaisuus: O (n) missä n = merkkien lukumäärä OTP: ssä Aputila: Sen lisäksi, että merkkijono, jolla on kaikki mahdolliset merkit, tarvitsemme O (n) -tilaa OTP: n pitämiseksi, jossa n = OTP: n merkkien lukumäärä, jos pidät geeksforgeeksista ja haluat osallistua, voit myös kirjoittaa artikkelin käyttämällä kirjoita.geeksforgeeks.org Tai lähetä artikkeli Katso artikkelisi, joka ilmestyy Geeksforgeeksin pääsivulla ja auta muita geekejä. Kirjoita kommentteja, jos löydät jotain virheellistä tai haluat jakaa lisätietoja yllä käsiteltyä aihetta.