ワンタイム パスワード (OTP) は、コンピュータ システムまたはその他のデジタル デバイス上で 1 回のログイン セッションまたはトランザクションにのみ有効なパスワードです。詳細については、を参照してください これ 。 アルゴリズム あらゆる可能性からランダムに文字を選択し、そこから必要な長さの文字列を生成します。 OTP は通常 6 ~ 7 文字の長さで、6 ~ 7 文字のランダム性により、安全なログイン方法がほぼ保証されます。
OTP は、Facebook、Google サインイン、Wifi、鉄道ポータル ログインなどの Web サイトで広く使用されています。
どのように生成されるのでしょうか?
OTP が生成されるときと同じアルゴリズムを使用する可能性が非常に高いです。偶然 (非常にまれですが) 生成された一意の文字列が以前に生成されており、別のコードに関連付けられている場合は、別のランダムな文字列が使用されます。現時点では、すべてのコードを一意に識別するためにランダムに生成される文字列は 6 つだけのようです。 6 つの文字列がすべて使い果たされる日が来ます。つまり、Web 関連のものでもランダム性に大きく依存しているのです。
確率 2 つの OTP の衝突
- OTP の長さは 6 で、OTP 内のすべての可能な文字のセット サイズは 62 です。したがって、OTP のペアの可能なセットの合計数は次のようになります。 62 12 。
- その一部は - [{AAAAAAAAA} {AAAAAAAAAAAAB} ..... {456789 456788} {456789 456789}]
- ただし、OTP の等しいペアの可能なセットは次のとおりです。 62 6 。その一部は - [{AAAAAAAAA} {AAAAAB AAAAAAB} ..... {456788 456788} {456789 456789}]
- したがって、 確率 2 つの OTP の衝突の割合は次のとおりです。 62 6 / 62 12 = 1/62 6 = 1 / 56800235584 = 1.7605561 -11
それで、 確率 2 つの OTP が衝突する確率は、地球上であなたの人生が存在する可能性と同じくらい低いです (宇宙と存在するすべてのものの始まりからの年数に対するあなたの生存年数の比)。したがって、yesOTP は静的パスワードよりもはるかに安全です。 実装
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
出力 (実行ごとに異なる場合があります):
文字列のJava値
Your OTP is - 8qOtzy
時間計算量: O(N) ここで、N = OTP の文字数 補助スペース: 可能なすべての文字を含む文字列とは別に、OTP を保持するには O(N) スペースが必要です (N = OTP の文字数) GeeksforGeeks が好きで、貢献したい場合は、次を使用して記事を書くこともできます。 write.geeksforgeeks.org または、記事を [email protected] にメールで送信してください。 GeeksforGeeks のメイン ページにあなたの記事が掲載されているのを見て、他のオタクを助けてください。間違った点を見つけた場合、または上記のトピックについてさらに詳しい情報を共有したい場合は、コメントを書いてください。