java.lang.Object
org.eclipse.milo.opcua.stack.core.util.NonceUtil

public class NonceUtil extends Object
  • Field Details

    • MINIMUM_NONCE_LENGTH

      public static final int MINIMUM_NONCE_LENGTH
      The minimum required nonce length for validation by validateNonce(ByteString).

      This is specified by OPC UA Part 4 to be least 32 bytes in the CreateSession and ActivateSession services.

      See Also:
  • Constructor Details

    • NonceUtil

      public NonceUtil()
  • Method Details

    • blockUntilSecureRandomSeeded

      public static void blockUntilSecureRandomSeeded() throws ExecutionException, InterruptedException
      Block until the SecureRandom instance has been seeded.
      Throws:
      ExecutionException - if seeding completed exceptionally.
      InterruptedException - if the current thread was interrupted.
    • blockUntilSecureRandomSeeded

      public static void blockUntilSecureRandomSeeded(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException
      Block for at most timeout waiting for the SecureRandom instance to be seeded.
      Parameters:
      timeout - the maximum time to wait.
      unit - the TimeUnit of the timeout argument.
      Throws:
      ExecutionException - if seeding completed exceptionally.
      InterruptedException - if the current thread was interrupted.
      TimeoutException - if the wait timed out.
    • secureRandomSeeded

      public static CompletionStage<Void> secureRandomSeeded()
      Get a CompletionStage that is completed when the SecureRandom instance has been seeded.
      Returns:
      a CompletionStage that is completed when the SecureRandom instance has been seeded.
    • enableSecureRandom

      public static void enableSecureRandom()
      Enable the use of a SecureRandom for nonce generation. This is the default.
    • disableSecureRandom

      public static void disableSecureRandom()
      Disable the use of a SecureRandom for nonce generation. Not recommended.
    • isSecureRandomEnabled

      public static boolean isSecureRandomEnabled()
      Returns:
      true is nonce generation uses a SecureRandom instance.
    • isSecureRandomSeeded

      public static boolean isSecureRandomSeeded()
      Returns:
      true if the SecureRandom instance is seeded and available.
    • generateNonce

      public static ByteString generateNonce(int length)
      Parameters:
      length - the length of the nonce to generate.
      Returns:
      a nonce of the given length.
    • generateNonce

      public static ByteString generateNonce(SecurityPolicy securityPolicy)
      Generate a nonce for the given SecurityPolicy.

      The length is determined by the policy: see getNonceLength(SecurityPolicy).

      Important: this should only be used to generate a nonce exchanged when establishing a secure channel. The nonce used by sessions has different length requirements.

      Parameters:
      securityPolicy - the SecurityPolicy to use when determining the nonce length.
      Returns:
      a nonce of the appropriate length for the given security policy.
    • validateNonce

      public static void validateNonce(ByteString nonce, SecurityPolicy securityPolicy) throws UaException
      Validate that nonce meets the minimum length requirement for securityPolicy and is non-zeroes.

      Important: this should only be used to validate the nonce exchanged when establishing a secure channel. The nonce used by sessions has different length requirements.

      Parameters:
      nonce - the nonce to validate.
      securityPolicy - the SecurityPolicy dictating the minimum nonce length.
      Throws:
      UaException - if nonce validation failed.
    • validateNonce

      public static void validateNonce(ByteString nonce) throws UaException
      Validate that nonce is at least MINIMUM_NONCE_LENGTH and is non-zeroes.
      Parameters:
      nonce - the nonce to validate.
      Throws:
      UaException - if nonce validation failed.
    • validateNonce

      public static void validateNonce(ByteString nonce, int minimumLength) throws UaException
      Validate that nonce is at least minimumLength and is non-zeroes.
      Parameters:
      nonce - the nonce to validate.
      minimumLength - the minimum required nonce length
      Throws:
      UaException - if nonce validation failed.