Java Code Examples for java.security.SecureRandom#nextBoolean()

The following examples show how to use java.security.SecureRandom#nextBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MagicSquareGenerator.java    From genesis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a magic square with a given size
 *
 * @param size the size of the magic square. If this value is even, it is
 * either increased or decreased by one before the square is calculated
 * @return the magic square with the given size
 */
public MagicSquare generate(int size) {
    //Square generation at this point only works using odd sizes
    if (!isOdd(size)) {
        //If the value is not odd, the value is increased or decreased by 1
        SecureRandom random = new SecureRandom();
        if (random.nextBoolean()) {
            size++;
        } else {
            size--;
        }
    }
    //The magic square is stored in a two dimensional array
    int[][] magicSquare = calculate(size);
    //The value of the square is stored in a different field for later use
    int value = calculateMagicSquareValue(magicSquare);
    //The object is returned
    return new MagicSquare(magicSquare, value, size);
}
 
Example 2
Source File: TestUtils.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
/**
 * ランダムに、大文字、小文字に変換する。
 * @param str
 * @return
 */
public static final String toRandomCase(final String str) {
    
    StringBuilder sb = new StringBuilder();
    try {
        SecureRandom random = new SecureRandom();
        final int len = str.length();
        for(int i=0; i < len; i++) {
            char c = str.charAt(i);
            if(random.nextBoolean()) {
                sb.append(String.valueOf(c).toLowerCase());
            } else {
                sb.append(String.valueOf(c).toUpperCase());
            }
        }
    } catch(Exception e) {
        throw new RuntimeException(e);
    }
    return sb.toString();
    
}
 
Example 3
Source File: PrngSlow.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    try {
        SecureRandom sr = null;
        sr = SecureRandom.getInstance("PRNG", "SunMSCAPI");
        long start = System.nanoTime();
        int x = 0;
        for(int i = 0; i < 10000; i++) {
            if (i % 100 == 0) System.err.print(".");
            if (sr.nextBoolean()) x++;
        };
        t = (System.nanoTime() - start) / 1000000000.0;
        System.err.println("\nSpend " + t + " seconds");
    } catch (Exception e) {
        // Not supported here, maybe not a Win32
        System.err.println("Cannot find PRNG for SunMSCAPI or other mysterious bugs");
        e.printStackTrace();
        return;
    }
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 4
Source File: PrngSlow.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    try {
        SecureRandom sr = null;
        sr = SecureRandom.getInstance("PRNG", "SunMSCAPI");
        long start = System.nanoTime();
        int x = 0;
        for(int i = 0; i < 10000; i++) {
            if (i % 100 == 0) System.err.print(".");
            if (sr.nextBoolean()) x++;
        };
        t = (System.nanoTime() - start) / 1000000000.0;
        System.err.println("\nSpend " + t + " seconds");
    } catch (Exception e) {
        // Not supported here, maybe not a Win32
        System.err.println("Cannot find PRNG for SunMSCAPI or other mysterious bugs");
        e.printStackTrace();
        return;
    }
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 5
Source File: SecureRandomDemo.java    From tutorials with MIT License 6 votes vote down vote up
public static void generateSecureRandomValues() {
    SecureRandom sr = new SecureRandom();

    int randomInt = sr.nextInt();
    long randomLong = sr.nextLong();
    float randomFloat = sr.nextFloat();
    double randomDouble = sr.nextDouble();
    boolean randomBoolean = sr.nextBoolean();

    IntStream randomIntStream = sr.ints();
    LongStream randomLongStream = sr.longs();
    DoubleStream randomDoubleStream = sr.doubles();

    byte[] values = new byte[124];
    sr.nextBytes(values);
}
 
Example 6
Source File: PrngSlow.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 7
Source File: CryptoHelper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static String pronounceable(SecureRandom random) {
    final int rand = random.nextInt(4);
    char[] output = new char[rand * 2 + (5 - rand)];
    boolean vowel = random.nextBoolean();
    for (int i = 0; i < output.length; ++i) {
        output[i] = vowel ? VOWELS[random.nextInt(VOWELS.length)] : CONSONANTS[random.nextInt(CONSONANTS.length)];
        vowel = !vowel;
    }
    return String.valueOf(output);
}
 
Example 8
Source File: CryptoHelper.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static String pronounceable(SecureRandom random) {
    final int rand = random.nextInt(4);
    char[] output = new char[rand * 2 + (5 - rand)];
    boolean vowel = random.nextBoolean();
    for (int i = 0; i < output.length; ++i) {
        output[i] = vowel ? VOWELS[random.nextInt(VOWELS.length)] : CONSONANTS[random.nextInt(CONSONANTS.length)];
        vowel = !vowel;
    }
    return String.valueOf(output);
}
 
Example 9
Source File: PrngSlow.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 10
Source File: PrngSlow.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 11
Source File: PrngSlow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 12
Source File: PrngSlow.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 13
Source File: PrngSlow.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 14
Source File: RandomUtils.java    From signald with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected SecureRandom initialValue() {
    SecureRandom rand = getSecureRandomUnseeded();

    // Let the SecureRandom seed it self initially
    rand.nextBoolean();

    return rand;
}
 
Example 15
Source File: PrngSlow.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    double t = 0.0;
    SecureRandom sr = null;
    sr = SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI");
    long start = System.nanoTime();
    for (int i = 0; i < 10000; i++) {
        if (i % 100 == 0) System.err.print(".");
        sr.nextBoolean();
    };
    t = (System.nanoTime() - start) / 1000000000.0;
    System.err.println("\nSpend " + t + " seconds");
    if (t > 5)
        throw new RuntimeException("Still too slow");
}
 
Example 16
Source File: Test_Ed25519.java    From ts3j with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception
{
    System.out.println("Test_Ed25519");
    SecureRandom sr = new SecureRandom();
    
    int trueC = 0;

    for (int i = 0; i < 500; i++)
    {

        // byte[] Seed = new byte[]{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0,
        // 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3};

        byte[] Seed = new byte[]{0x34, (byte) 0x9E, (byte) 0x74, (byte) 0x52, (byte) 0xFA, (byte) 0x01, (byte) 0x8A, (byte) 0xD0, (byte) 0xE1, (byte) 0xE7, (byte) 0xA6, (byte) 0x3A, (byte) 0x77,
                (byte) 0xF9, (byte) 0xE7, (byte) 0x78, (byte) 0x29, (byte) 0x30, (byte) 0x48, (byte) 0xE2, (byte) 0x93, (byte) 0xDE, (byte) 0x58, (byte) 0x3C, (byte) 0xC6, (byte) 0x84,
                (byte) 0xFF, (byte) 0x56, (byte) 0xC7, (byte) 0x4D, (byte) 0xF8, (byte) 0xA4};

        sr.nextBytes(Seed);
        
        //byte posNext = (byte)(sr.nextInt() & 0x1F);
        Boolean doCheck = sr.nextBoolean();

        byte[] pk = Ed25519.PublicKeyFromSeed(Seed);
        byte[] sk = Ed25519.ExpandedPrivateKeyFromSeed(Seed);
        byte[] sig = Ed25519.Sign(new byte[32], sk);
        byte[] sss = new byte[32];
        
        if(doCheck)
        {
            /*byte oldVal = sss[posNext];
            byte newVal = 0;
            do
            {
                newVal = (byte)(sr.nextInt() & 0xFF);
            }
            while(oldVal == newVal);
            sss[posNext] = newVal;     */   
            
            sr.nextBytes(sss);
        }
        else
        {
             
        }
        
        Boolean bb = Ed25519.Verify(sig, sss, pk);            

       // System.out.println("TEST: " + i);
       // System.out.println("bb : " + bb);
        if (!doCheck || (doCheck && !bb))
        {
            trueC++;
           // System.out.println("PK : " + bytesToHex(pk));
           // System.out.println("SK : " + bytesToHex(sk));
           // System.out.println("sig : " + bytesToHex(sig));
        }

    }
    
    System.out.println("True Count: " + trueC);


}
 
Example 17
Source File: Utils.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
@Override
protected SecureRandom initialValue() {
    final SecureRandom result = new SecureRandom();
    result.nextBoolean(); // Force seeding
    return result;
}
 
Example 18
Source File: StringUtil.java    From Puff-Android with MIT License 4 votes vote down vote up
public static boolean getRandBool() {
    SecureRandom random = new SecureRandom();
    return random.nextBoolean();
}
 
Example 19
Source File: Sorting.java    From streamsupport with GNU General Public License v2.0 4 votes vote down vote up
void build(int[] a, int m, SecureRandom random) {
    int x = 0, y = 0;
    for (int i = 0; i < a.length; i++) {
        a[i] = random.nextBoolean() ? (x += 2) : (y += 2);
    }
}
 
Example 20
Source File: GenericStringObfuscator.java    From genesis with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Insert garbage code in every function
 *
 * @param classObject the class to insert the code in
 * @param loc the lines of code (loc) that are possibly added
 * @param amount the amount of additions that should be done if an insertion
 * is done
 * @return the modified <code>IClass</code> object
 */
public IClass insertCode(IClass classObject, List<String> loc, int amount) {
    //TODO avoid sending a LOC list, as this can be generated in this class(?)
    //Creates a secure random object
    SecureRandom random = new SecureRandom();
    //Iterates through all functions within the given class object
    for (IFunction function : classObject.getFunctions()) {
        //Initiate the new body as an empty string, to which new lines are appended
        String newBody = "";
        //The boolean which decides if code is inserted at a given point
        boolean insertLine;
        //The scanner reads the whole function body
        Scanner scanner = new Scanner(function.getBody());
        //Iterate through each line 
        while (scanner.hasNextLine()) {
            //Get a single line
            String line = scanner.nextLine();
            //Loop as many lines of code as amount is in size
            for (int i = 0; i < amount; i++) {
                //Set the insert line boolean to either true or false (making the addition of a line roughly 50% of the size of amount, but not always the same
                insertLine = random.nextBoolean();
                //If the boolean is true and the provided list is not empty, a new line of code is added to the function's body
                if (insertLine && loc.size() > 0) {
                    //Get a random index
                    int randomIndex = random.nextInt(loc.size());
                    //Add the random line of code at the random index to the body of the function
                    newBody += loc.get(randomIndex);
                    //Remove the entry at the given index to avoid reusing it
                    loc.remove(randomIndex);
                }
            }
            //Add a newline character, since it is removed by the scanner
            newBody += line + "\n";
        }
        //Close the scanner object
        scanner.close();
        //Set the new body in the function
        function.setBody(newBody);
    }
    //Return the changed class object
    return classObject;
}