SshDaemon: Add implementation of InsecureBouncyCastleRandom.random()
Copied from org.apache.sshd.common.random.BouncyCastleRandom Change-Id: Ie12d7fd85eeeb38c380e73f2006026572128b7a9
This commit is contained in:
@@ -439,8 +439,29 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
|
||||
@Override
|
||||
public int random(int n) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
if (n > 0) {
|
||||
if ((n & -n) == n) {
|
||||
return (int)((n * (long) next(31)) >> 31);
|
||||
}
|
||||
int bits, val;
|
||||
do {
|
||||
bits = next(31);
|
||||
val = bits % n;
|
||||
} while (bits - val + (n-1) < 0);
|
||||
return val;
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
final protected int next(int numBits) {
|
||||
int bytes = (numBits+7)/8;
|
||||
byte next[] = new byte[bytes];
|
||||
int ret = 0;
|
||||
random.nextBytes(next);
|
||||
for (int i = 0; i < bytes; i++) {
|
||||
ret = (next[i] & 0xFF) | (ret << 8);
|
||||
}
|
||||
return ret >>> (bytes*8 - numBits);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user