最终静态SecureRandom.getInstanceStrong()

时间:2014-04-22 21:19:46

标签: java

这是我带注释的代码。

class RandomThing{

// This is one way to do it, but I want more randomness.
    protected final static Random rand = new Random();

// This is how I do it with SecureRandom.
    protected final static Random rand = new SecureRandom();

// Here is the best way I can get a strong instance, but rand is no longer final.
    protected static Random rand = null;

    static {
        try {
            rand = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException ex) {
            // Blow up.
        }
    }
}

我怎么能有这样的东西?

protected final static Random rand = SecureRandom.getInstanceStrong();
// not okay because getInstanceStrong throws NoSuchAlgorithmException

0 个答案:

没有答案