Skip to content

secureRandom

Aichaellee edited this page Apr 29, 2018 · 1 revision

  Java中的密码安全随机

  除了使用libsodium(推荐),也可以直接使用Java的SecureRandom类:

  SecureRandom csprng = new SecureRandom();

  byte[] randomBytes = new byte[32];

  csprng.nextBytes(randombytes);

  注意:不要在Linux上使用SecureRandom.getInstanceStrong(),不要被名称误导,它等同于读取/dev/random,这个是不安全的。Java8中new SecureRandom()默认读取/dev/urandom,这才是你需要的。

Clone this wiki locally