将 SecureRandom 和 UUID 转换为 int (java)?

发布于 2024-09-15 20:25:55 字数 713 浏览 6 评论 0原文

我在接受字符串(一个人的名字)并返回一个唯一的整数时遇到问题,它不断地通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无尽的现实 2024-09-22 20:25:55

您不能使用该人的姓名来获取 SecureRandom 对象。它需要随机数生成器实现中的名称。您可以使用“SHA1PRNG”,因为它是默认可用的。然后,您可以使用 name.getBytes() 为随机生成器播种,然后获取下一个随机数。

来自 Javadoc:

public static SecureRandom getInstance(String algorithm)
抛出 NoSuchAlgorithmException 异常
生成一个 SecureRandom 对象,该对象...剪切...

参数:
算法 - PRNG 算法的名称。请参阅 Java 加密体系结构 API 规范和规范中的附录 A。有关标准 PRNG 算法名称的信息参考。

退货:
新的 SecureRandom 对象。
投掷:
NoSuchAlgorithmException - 如果 PRNG 算法在调用者的环境中不可用。
自从:
1.2

您可以跳过其余部分,因为哈希值与算法一样好,向其中添加其他内容很难使其更安全。

类似于:

static int getUUID(String name) throws NoSuchAlgorithmException {
    SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
    srA.setSeed(name.getBytes());
    return new Integer(srA.nextInt());
}

我通常会使用 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 :

static int getUUID(String name) throws NoSuchAlgorithmException {
    SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
    srA.setSeed(name.getBytes());
    return new Integer(srA.nextInt());
}

I would have normally used a MessageDigest, but I must admit this is pretty tight.

迷离° 2024-09-22 20:25:55

如果要将字符串映射到数字,则需要哈希函数或摘要。看一下 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

誰認得朕 2024-09-22 20:25:55

怎么样:

    static final long getUUID() throws NoSuchAlgorithmException {
        SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
        srA.setSeed(RandomUtils.nextLong());
        return new Long(srA.nextInt());
    }


    static final String getFixedLengthUUID() {
      try {
        return Long.toHexString(Math.abs(getUUID())) + Long.toHexString(Math.abs(getUUID()));
      } catch (Exception e) {
        return null;
    }
}

这会打印出 16 个字符的数字:

3fc2ce6b4da08fe0
3fba60e0b8387c58
78cb09b976b112d3
3c28f91c473e29ba
203af64d1e747af6
4934d0b4342b8f30
3b576a97ba8f857
76d49522cecc5c4
5cf650ed72a55b50
1e89acfb4a4589f3

What about:

    static final long getUUID() throws NoSuchAlgorithmException {
        SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
        srA.setSeed(RandomUtils.nextLong());
        return new Long(srA.nextInt());
    }


    static final String getFixedLengthUUID() {
      try {
        return Long.toHexString(Math.abs(getUUID())) + Long.toHexString(Math.abs(getUUID()));
      } catch (Exception e) {
        return null;
    }
}

This prints out numbers with 16 characters:

3fc2ce6b4da08fe0
3fba60e0b8387c58
78cb09b976b112d3
3c28f91c473e29ba
203af64d1e747af6
4934d0b4342b8f30
3b576a97ba8f857
76d49522cecc5c4
5cf650ed72a55b50
1e89acfb4a4589f3
寄离 2024-09-22 20:25:55

根据文档, NoSuchAlgorithmException被抛出

...当请求特定的加密算法但在环境中不可用时。

您需要为 getInstance(String Algorithm),而不是人名。

如果您只需要一个随机数,请尝试将代码中的该行替换为

SecureRandom srA = new SecureRandom();

如果您不关心数字的随机性并且您只是想为名称创建一个唯一的整数,那么我认为您需要的一切是调用 hashCode 的名称。

According to the docs, NoSuchAlgorithmException is thrown

...when a particular cryptographic algorithm is requested but is not available in the environment.

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

SecureRandom srA = new SecureRandom();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文