Java/Security/Secure Random

Материал из Java эксперт
Перейти к: навигация, поиск

Generate Seed

  
import java.security.SecureRandom;
public class Main {
  public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    int seedByteCount = 10;
    byte[] seed = sr.generateSeed(seedByteCount);
  }
}





Generating a Secure Random Number

  
import java.security.SecureRandom;
public class Main {
  public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    byte[] bytes = new byte[1024 / 8];
    sr.nextBytes(bytes);
  }
}





Secure Random: SHA1PRNG

 
import java.security.SecureRandom;
import java.security.Security;
public class MainClass {
  public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG");
    rng.setSeed(711);
    int numberToGenerate = 999;
    byte randNumbers[] = new byte[numberToGenerate];
    rng.nextBytes(randNumbers);
    for(int j=0; j<numberToGenerate; j++) {
        System.out.print(randNumbers[j] + " ");
    }
  }
}