gpt4 book ai didi

java - Java的SecureRandom循环

转载 作者:搜寻专家 更新时间:2023-10-31 20:28:51 25 4
gpt4 key购买 nike

PRNG 通常有一个循环,之后生成的随机数会重复。 Java的SecureRandom在创建SecureRandom实例时的循环是怎样的:

SecureRandom random = 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++) {
// 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]++;
}

没有计数器增加,但内部状态只是用输出更新。

关于java - Java的SecureRandom循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19000536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com