2016-11-29 106 views
0

有很多问题询问SecureRandom的具体启动是否“好”,但我找不到经验法则。创建“良好”SecureRandom的最佳方式是什么?

什么是创建“良好”随机SecureRandom的最佳方式?

// Fast 
// Is it a good random? 
SecureRandom secureRandom = new SecureRandom()? 

// Freezes for a few seconds after being used several times - until getting a random enough seed. 
// See http://stackoverflow.com/questions/137212/how-to-solve-performance-problem-with-java-securerandom#comment68934647_137212 
// Is it a good random? 
SecureRandom secureRandom = new SecureRandom(SecureRandom.getSeed(20))? 

// Freezes for a very long time. Waited minutes and still no return :(
SecureRandom secureRandom = new SecureRandom.getInstanceStrong()? 

Other? 

回答

0

我相信,除非你有特殊要求,否则他们都足够好。

getSeed()的医生说:

此方法仅包括为了向后兼容。鼓励调用者 使用其中一种替代getInstance方法来 获取SecureRandom对象,然后调用generateSeed方法为 从该对象获取种子字节。

您当然可以按照说明准确地进行操作,但我不确定您的其他示例是否有意义。

我不记得在哪里,但我很久以前就读过,最好明确地给出算法。所以我通常做

SecureRandom.getInstance("SHA1PRNG") 

为SHA1算法; PRNG用于伪随机数发生器。虽然SHA1因为一般的密码学目的而被磨损,但是当我们想要将它们用于密码密钥时,我被告知它对于随机数仍然很好。

相关问题