生成一次性密码或唯一标识URL

一次性密码(OTP)是在计算机系统或其他数字设备上仅对一次登录会话或事务有效的密码。有关更多详细信息,请参阅 . 算法 从我们所有的可能性中随机选取字符,并从中生成所需长度的字符串。OTP的长度通常为6-7个字符,6-7个字符的随机性几乎保证了登录的安全性。

null

应用

  • OTP广泛应用于Facebook、谷歌登录、Wifi接入、铁路门户登录等网站。
  • 即使是极客 集成电路设备 对于通过它编译的所有代码都有一个唯一的字符串。例如 https://ide.geeksforgeeks.org/Ks84Ck 有唯一的字符串—— “Ks84Ck” 在最后,这是唯一的这段代码。

它是如何产生的? 他们很有可能使用与生成OTP相同的算法。如果碰巧(非常罕见)生成的唯一字符串之前已经生成,并且与不同的代码关联,则使用另一个随机字符串。

根据now,似乎只有六个字符串随机生成,用于所有代码的唯一标识。总有一天,所有可能的六个字符串都会用尽。所以是的,即使是与网络相关的东西也严重依赖于随机性。

两个OTP的碰撞概率

  • OTP的长度为6,OTP中所有可能字符的设置大小为62。所以这对OTP的可能集合的总数是 62 12 .
  • 其中一些是–[{aaaaaa,aaaaaa},{aaaaaa,aaaaaab},…{456789,456788},{456789,456789}]
  • 但可能的对等OTP对集是: 62 6. .其中一些是–[{aaaaaa,aaaaaa},{aaaaab,aaaaab},…{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<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


输出(每次运行可能不同):

Your OTP is - 8qOtzy

时间复杂性: O(N),其中N=OTP中的字符数

辅助空间: 除了包含所有可能字符的字符串外,我们还需要O(N)空间来保存OTP,其中N=OTP中的字符数

本文由 拉希特·贝尔瓦里亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享