Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycle of SecureRandom of Java

Tags:

java

random

prng

PRNGs usually have a cycle after which the generated random numbers do repeat. What's the cycle of SecureRandom of Java when the instance of SecureRandom is created as follows:

SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

like image 924
user2814390 Avatar asked Nov 02 '22 14:11

user2814390


1 Answers

I'm a bit confused. I had a look into the code of sun.security.provider.SecureRandom of the openjdk. Here the internal state is updated as follows:

digest.update(state);
output = digest.digest();
updateState(state, output);

[...]

private static void updateState(byte[] state, byte[] output) {
    int last = 1;
    int v = 0;
    byte t = 0;
    boolean zf = false;

    // state(n + 1) = (state(n) + output(n) + 1) % 2^160;
    for (int i = 0; i < state.length; i++) {
        // Add two bytes
        v = (int)state[i] + (int)output[i] + last;
        // Result is lower 8 bits
        t = (byte)v;
        // Store result. Check for state collision.
        zf = zf | (state[i] != t);
        state[i] = t;
        // High 8 bits are carry. Store for next iteration.
        last = v >> 8;
    }

    // Make sure at least one bit changes!
    if (!zf)
       state[0]++;
}

No counter is incremented but the internal state is simply updated with the output.

like image 158
user2814390 Avatar answered Nov 15 '22 05:11

user2814390