将 SecureRandom 和 UUID 转换为 int (java)?
我在接受字符串(一个人的名字)并返回一个唯一的整数时遇到问题,它不断地通过 catch 函数,我不知道为什么,除了我编写 SecureRandom 的方式不起作用,但它非常好令人困惑。我对编程很陌生,所以请友善!
public static int uinqueID(String name){
try{
SecureRandom srA = SecureRandom.getInstance(name);
Integer randomA = new Integer(srA.nextInt());
System.out.println(randomA);
UUID uuidA = UUID.randomUUID();
String randomNum2 = uuidA.toString();
System.out.println(randomNum2);
int randomB = Integer.valueOf(randomNum2);
int uniqueID = randomA + randomB;
return uniqueID;
} catch(NoSuchAlgorithmException e) {
System.err.println("I failed");
}
return -1;
}
我得到的输出是: 我失败了 -1
谢谢你的帮助!
I am having problems taking in a string (a persons name), and getting back a unique integer, it keeps going through the catch function, and I dont know why, other than the way I wrote SecureRandom is not working, but it is very confusing. I am very new to programming so please be kind!
public static int uinqueID(String name){
try{
SecureRandom srA = SecureRandom.getInstance(name);
Integer randomA = new Integer(srA.nextInt());
System.out.println(randomA);
UUID uuidA = UUID.randomUUID();
String randomNum2 = uuidA.toString();
System.out.println(randomNum2);
int randomB = Integer.valueOf(randomNum2);
int uniqueID = randomA + randomB;
return uniqueID;
} catch(NoSuchAlgorithmException e) {
System.err.println("I failed");
}
return -1;
}
The output I am getting is:
I failed
-1
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能使用该人的姓名来获取 SecureRandom 对象。它需要随机数生成器实现中的名称。您可以使用“SHA1PRNG”,因为它是默认可用的。然后,您可以使用 name.getBytes() 为随机生成器播种,然后获取下一个随机数。
来自 Javadoc:
public static SecureRandom getInstance(String algorithm)
抛出 NoSuchAlgorithmException 异常
生成一个 SecureRandom 对象,该对象...剪切...
参数:
算法 - PRNG 算法的名称。请参阅 Java 加密体系结构 API 规范和规范中的附录 A。有关标准 PRNG 算法名称的信息参考。
退货:
新的 SecureRandom 对象。
投掷:
NoSuchAlgorithmException - 如果 PRNG 算法在调用者的环境中不可用。
自从:
1.2
您可以跳过其余部分,因为哈希值与算法一样好,向其中添加其他内容很难使其更安全。
类似于:
我通常会使用 MessageDigest,但我必须承认这非常严格。
You cannot use the name of the person to get a SecureRandom object. It expects the name of in implementation of a random number generator. You can use "SHA1PRNG" as that is default available. You can then seed the random generator with the name.getBytes() and then get the next random number.
From the Javadoc:
public static SecureRandom getInstance(String algorithm)
throws NoSuchAlgorithmException
Generates a SecureRandom object that ...snip...
Parameters:
algorithm - the name of the PRNG algorithm. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard PRNG algorithm names.
Returns:
the new SecureRandom object.
Throws:
NoSuchAlgorithmException - if the PRNG algorithm is not available in the caller's environment.
Since:
1.2
You can skip the rest because the hash is as good as the algorithm and adding other things to it hardly make it more secure.
something like :
I would have normally used a MessageDigest, but I must admit this is pretty tight.
如果要将字符串映射到数字,则需要哈希函数或摘要。看一下 java.security.MessageDigest
If you want to map a String to a number, you need a hash function, or a digest. Take a look at java.security.MessageDigest
怎么样:
这会打印出 16 个字符的数字:
What about:
This prints out numbers with 16 characters:
根据文档, NoSuchAlgorithmException被抛出
您需要为
getInstance(String Algorithm)
,而不是人名。如果您只需要一个随机数,请尝试将代码中的该行替换为
如果您不关心数字的随机性并且您只是想为名称创建一个唯一的整数,那么我认为您需要的一切是调用 hashCode 的名称。
According to the docs, NoSuchAlgorithmException is thrown
You need to specify a Random Number Generator (RNG) algorithm to
getInstance(String algorithm)
, not a person's name.If you just need a random number, try replacing that line in your code with
If you don't care how random the number is and you're just trying to create a unique integer for the name, then I think all you'll need is to call hashCode on the name.