Class RandomSource
- java.lang.Object
-
- io.heraldprox.herald.sensor.datatype.random.RandomSource
-
- Direct Known Subclasses:
BlockingSecureRandom
,BlockingSecureRandomSingleton
,NonBlockingCSPRNG
,NonBlockingPRNG
,NonBlockingSecureRandom
public abstract class RandomSource extends java.lang.Object
Source of random data. Override and implement nextBytes() method to provide custom random source that is application specific.
-
-
Field Summary
Fields Modifier and Type Field Description RingBuffer
entropy
Entropy gathered from external sources via addEntropy()
-
Constructor Summary
Constructors Constructor Description RandomSource()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addEntropy(byte value)
Contribute entropy from external source.void
addEntropy(long value)
Contribute entropy from external source.void
addEntropy(java.lang.String value)
Collect entropy from external sources.protected static Data
hash(Data data)
Cryptographic hash function : SHA256 Reverts to non-cryptographic Java hash function in the unlikely event that SHA256 is not supported on the system.abstract void
nextBytes(byte[] bytes)
Get random bytes from the random source.int
nextInt()
Get random int value from random source.long
nextLong()
Get random long value from random source.protected long
useEntropy()
Spend entropy from external source.void
useEntropy(int bytes, Data sink)
Spend entropy by transferring entropy data to sink.
-
-
-
Field Detail
-
entropy
public final RingBuffer entropy
Entropy gathered from external sources via addEntropy()
-
-
Method Detail
-
addEntropy
public void addEntropy(byte value)
Contribute entropy from external source.- Parameters:
value
- entropy data
-
addEntropy
public void addEntropy(long value)
Contribute entropy from external source.- Parameters:
value
- entropy data
-
addEntropy
public void addEntropy(@Nullable java.lang.String value)
Collect entropy from external sources. Use detected BLE MAC addresses as an entropy source as these addresses are mostly, if not all, derived from disparate SecureRandom instances.- Parameters:
value
- BLE MAC address of target device, only the hex digits [0-9A-Z] are used
-
useEntropy
protected long useEntropy()
Spend entropy from external source. Entropy is cleared upon use.- Returns:
- Entropy gathered by addEntropy()
-
useEntropy
public void useEntropy(int bytes, @NonNull Data sink)
Spend entropy by transferring entropy data to sink. Spent entropy is cleared and not reused.- Parameters:
bytes
- Maximum number of bytes to transfer if available.sink
- Target data buffer for transferring entropy data.
-
nextBytes
public abstract void nextBytes(@NonNull byte[] bytes)
Get random bytes from the random source.- Parameters:
bytes
- Fill byte array with random data.
-
nextInt
public int nextInt()
Get random int value from random source.- Returns:
- Random int value derived from 4 random bytes.
-
nextLong
public long nextLong()
Get random long value from random source.- Returns:
- Random long value derived from 8 random bytes.
-
-