Class DefaultNonce

java.lang.Object
com.globalmentor.security.DefaultNonce
All Implemented Interfaces:
Nonce

public class DefaultNonce extends Object implements Nonce
A nonce that uses the current time, the date, a secret key, and a random number. This implementation stores information in the form: time:privateKey:value
Author:
Garret Wilson
  • Field Details

    • DELIMITER

      public static final char DELIMITER
      The character that delimits fields in the nonce.
      See Also:
    • DELIMITER_STRING

      protected static final String DELIMITER_STRING
      The string version of the character that delimits fields in the nonce.
    • RANDOM

      protected static final Random RANDOM
      The shared random number factory.
  • Constructor Details

    • DefaultNonce

      public DefaultNonce(String privateKey)
      Constructs a new nonce with generated values.
      Parameters:
      privateKey - The private key stored in the nonce.
    • DefaultNonce

      protected DefaultNonce(String privateKey, Date time, long value) throws IllegalArgumentException
      Constructs a nonce from existing information.
      Parameters:
      privateKey - The private key stored in the nonce. This key must not contain any occurrences of the delimiter.
      time - The time stored in the nonce.
      value - A random value held by the nonce.
      Throws:
      IllegalArgumentException - if the private key contains an occurrence of the delimiter.
  • Method Details

    • getPrivateKey

      public String getPrivateKey()
      Specified by:
      getPrivateKey in interface Nonce
      Returns:
      The private key stored in the nonce.
    • getTime

      public Date getTime()
      Specified by:
      getTime in interface Nonce
      Returns:
      The time stored in the nonce.
    • getValue

      public long getValue()
      Returns:
      A random value held by the nonce.
    • createNonce

      public DefaultNonce createNonce(String string) throws SyntaxException
      Creates a nonce from a formatted string.
      Parameters:
      string - The string containing the nonce.
      Returns:
      A nonce containing values that would generate the same string as the one given.
      Throws:
      SyntaxException - if the given string does not have the correct format for this type of nonce.
    • toString

      public String toString()
      Specified by:
      toString in interface Nonce
      Overrides:
      toString in class Object
      Returns:
      A string representation of the nonce, suitable for serialization.