SecureRandom safe seed in Java

前端 未结 3 767
梦谈多话
梦谈多话 2020-12-14 22:52

Is this piece of code safe?

 SecureRandom randomizer = new SecureRandom(String.valueOf(new Date().getTime()).getBytes());

Is this the right

3条回答
  •  孤街浪徒
    2020-12-14 23:28

    I think it is best to let the SecureRandom seed itself. This is done by calling nextBytes immediately after it's creation (calling setSeed will prevent this).

    final byte[] dummy = new byte[512];
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.nextBytes(dummy);
    

    You want to use SHA1PRNG because it guarantees a fast non-blocking implementation even on Linux, where the default is not.

提交回复
热议问题