Class RandomSource

    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • entropy

        public final RingBuffer entropy
        Entropy gathered from external sources via addEntropy()
    • Constructor Detail

      • RandomSource

        public RandomSource()
    • 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.
      • hash

        @NonNull
        protected static Data hash​(@NonNull
                                   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. This should never happen as SHA256 must be supported in all Java implementations.