问题描述
PRNG通常有一个循环,之后生成的随机数会重复。如下创建的SecureRandom的实例时什么的Java的SecureRandom的周期:
SecureRandom的随机= SecureRandom.getInstance( SHA1PRNG);
我有点困惑。我查看了openjdk的sun.security.provider.SecureRandom的代码。这里内部状态更新如下:
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 ++){
//添加两个字节
v =(int)state [i] +(int)output [i] +最后;
//结果是低8位
t =(字节)v;
//存储结果。检查状态冲突。
zf = zf | (state [i]!= t);
state [i] = t;
//高8位进位。存储下一次迭代。
last = v>> 8;
}
//确保至少有一位变化!
if(!zf)
state [0] ++;
}
没有计数器递增,但内部状态只是用输出更新。 / p>
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");
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.
这篇关于Java的SecureRandom循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!