Java by API/java.security/SecureRandom

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

new SecureRandom()

 
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
/*
 */
public class MainClass {
  public static void main(String[] unused) {
    Random prng = new SecureRandom();  // self-seeding
    System.out.println(BigInteger.probablePrime(10, prng));
    }
 
}





SecureRandom: getInstance(String algorithm) throws NoSuchAlgorithmException

  
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();
    long seed = 01;
    if (args.length > 1)
      seed = (new Long("1111111111")).longValue();
    SecureRandom srand = SecureRandom.getInstance("SHA1PRNG");
    if (seed != 01)
      srand.setSeed(seed);
    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);
    System.out.println(new String(bytes));
  }
}





SecureRandom: nextBytes(byte[] bytes)

  

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();
    long seed = 01;
    if (args.length > 1)
      seed = (new Long("1111111111")).longValue();
    SecureRandom srand = SecureRandom.getInstance("SHA1PRNG");
    if (seed != 01)
      srand.setSeed(seed);
    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);
    System.out.println(new String(bytes));
  }
}





SecureRandom: setSeed(long seed)

  
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();
    long seed = 01;
    if (args.length > 1)
      seed = (new Long("1111111111")).longValue();
    SecureRandom srand = SecureRandom.getInstance("SHA1PRNG");
    if (seed != 01)
      srand.setSeed(seed);
    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);
    System.out.println(new String(bytes));
  }
}