Example usage for java.security SecureRandom getInstance

List of usage examples for java.security SecureRandom getInstance

Introduction

In this page you can find the example usage for java.security SecureRandom getInstance.

Prototype

public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

    System.out.println(sr.getAlgorithm());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

    int seedByteCount = 10;
    byte[] seed = sr.generateSeed(seedByteCount);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

    byte[] bytes = new byte[1024 / 8];
    sr.nextBytes(bytes);/*from  w  w  w. jav a  2s .  c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

    System.out.println(Arrays.toString(sr.generateSeed(8)));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

    byte[] bytes = new byte[8];
    sr.nextBytes(bytes);/*from  w  w w  . j a v a  2  s .c o  m*/
    System.out.println(Arrays.toString(bytes));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Random randomGenerator = SecureRandom.getInstance("SHA1PRNG");
    BigInteger randomInteger = new BigInteger(1024, randomGenerator);
    System.out.println(randomInteger);
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = SecureRandom.getInstance("SHA1PRNG");
    srand.setSeed(new byte[] { 1, 2, 3, 4 });
    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);/*ww  w  . java 2  s. c o  m*/
    System.out.println(new String(bytes));
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG");
    rng.setSeed(711);//from   ww w. j  av  a 2 s.c  o m

    int numberToGenerate = 999;
    byte randNumbers[] = new byte[numberToGenerate];

    rng.nextBytes(randNumbers);
    for (int j = 0; j < numberToGenerate; j++) {
        System.out.print(randNumbers[j] + " ");
    }

}

From source file:MainClass.java

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);/*from  www.  j a va2 s  .  c o  m*/
    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);
    System.out.println(new String(bytes));
}

From source file:org.jpwgen.Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    CommandLineParser parser = new PosixParser();
    HelpFormatter formatter = new HelpFormatter();

    Options options = new Options();

    options.addOption("l", "lowercase", false, "Include lowercase alpha characters");

    options.addOption("u", "uppercase", false, "Include uppercase alpha characters");

    options.addOption("d", "digits", false, "Include digits");
    options.addOption("p", "punctuation", false, "Include punctuation");

    options.addOption("n", "number", true, "Number of passwords to generate");

    options.addOption("L", "length", true, "Length of the generated passwords");

    try {//  w  w  w . j a  v  a2 s.c  o  m
        CommandLine cmd = parser.parse(options, args);

        boolean hasLower = cmd.hasOption('l');
        boolean hasUpper = cmd.hasOption('u');
        boolean hasDigits = cmd.hasOption('d');
        boolean hasPunctuation = cmd.hasOption('p');
        boolean hasLength = cmd.hasOption('L');
        boolean hasNumber = cmd.hasOption('n');

        // Check for options
        if (hasLower || hasUpper || hasDigits || hasPunctuation) {

            PasswordGenerator generator;
            generator = new PasswordGenerator(SecureRandom.getInstance("SHA1PRNG"));

            PasswordChars passwordChars = new PasswordChars(hasLower, hasUpper, hasDigits, hasPunctuation);

            // Number of passwords to generate by default
            int number = 10;

            // Length of passwords generated by default
            int length = 10;

            // If length has been specified by the user, use it
            // in place of the default value.
            if (hasLength) {
                try {
                    length = Integer.parseInt(cmd.getOptionValue('L'));
                } catch (NumberFormatException e) {
                    System.err.println("Error: The argument to length must be a number");
                    formatter.printHelp("pwgen", options);
                    System.exit(-1);
                }
            }

            // If number has been specified by the user, use it
            // in place of the default value.
            if (hasNumber) {
                try {
                    number = Integer.parseInt(cmd.getOptionValue('n'));
                } catch (NumberFormatException e) {
                    System.err.println("Error: The argument to number must be a number");
                    formatter.printHelp("pwgen", options);
                    System.exit(-1);
                }
            }

            final List<String> passwords;

            passwords = generator.generateMultiple(passwordChars, number, length);

            for (String password : passwords) {
                System.out.println(password);
            }
        } else {
            // If none found, print the usage message.
            formatter.printHelp("pwgen", options);
        }
    } catch (ParseException e) {
        System.err.println("Error parsing the commandline: " + e.getMessage());
    }
}