RandomBitsSupplierpublic class RandomBitsSupplier extends Object implements SHA1_DataThe static class providing access on Linux platform
to system means for generating true random bits.
The source for true random bits is one of Linux's devices "/dev/urandom/" or
"/dev/random" depends on which one is available; if both the first is used.
If no device available the service is not available,
that is, provider shouldn't register the algorithm.
|
Fields Summary |
---|
private static FileInputStream | bisBufferedInputStream to read from device | private static File | randomFileFile to connect to device | private static boolean | serviceAvailablevalue of field is "true" only if a device is available |
Methods Summary |
---|
private static synchronized byte[] | getLinuxRandomBits(int numBytes)On the Linux platform with "random" devices available,
the method reads random bytes from the device.
In case of any runtime failure ProviderException gets thrown.
byte[] bytes = new byte[numBytes];
int total = 0;
int bytesRead;
int offset = 0;
try {
for ( ; ; ) {
bytesRead = bis.read(bytes, offset, numBytes-total);
// the below case should not occur because /dev/random or /dev/urandom is a special file
// hence, if it is happened there is some internal problem
//
if ( bytesRead == -1 ) {
throw new ProviderException(
Messages.getString("security.193") ); //$NON-NLS-1$
}
total += bytesRead;
offset += bytesRead;
if ( total >= numBytes ) {
break;
}
}
} catch (IOException e) {
// actually there should be no IOException because device is a special file;
// hence, there is either some internal problem or, for instance,
// device was removed in runtime, or something else
//
throw new ProviderException(
Messages.getString("security.194"), e ); //$NON-NLS-1$
}
return bytes;
| public static byte[] | getRandomBits(int numBytes)The method returns byte array of requested length provided service is available.
ProviderException gets thrown otherwise.
if ( numBytes <= 0 ) {
throw new IllegalArgumentException(Messages.getString("security.195", numBytes)); //$NON-NLS-1$
}
if ( !serviceAvailable ) {
throw new ProviderException(
Messages.getString("security.196")); //$NON-NLS-1$
}
return getLinuxRandomBits(numBytes);
| static boolean | isServiceAvailable()The method is called by provider to determine if a device is available.
AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
for ( int i = 0 ; i < DEVICE_NAMES.length ; i++ ) {
File file = new File(DEVICE_NAMES[i]);
try {
if ( file.canRead() ) {
// BEGIN android-modified
bis = new FileInputStream(file);
// END android-modified
randomFile = file;
return null;
}
} catch (FileNotFoundException e) {
}
}
return null;
}
}
);
serviceAvailable = (bis != null);
return serviceAvailable;
|
|