Class DefaultNonce

  • All Implemented Interfaces:
    Nonce

    public class DefaultNonce
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static char DELIMITER
      The character that delimits fields in the nonce.
      protected static java.lang.String DELIMITER_STRING
      The string version of the character that delimits fields in the nonce.
      protected static java.util.Random RANDOM
      The shared random number factory.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        DefaultNonce​(java.lang.String privateKey)
      Constructs a new nonce with generated values.
      protected DefaultNonce​(java.lang.String privateKey, java.util.Date time, long value)
      Constructs a nonce from existing information.
    • Field Detail

      • DELIMITER

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

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

        protected static final java.util.Random RANDOM
        The shared random number factory.
    • Constructor Detail

      • DefaultNonce

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

        protected DefaultNonce​(java.lang.String privateKey,
                               java.util.Date time,
                               long value)
                        throws java.lang.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:
        java.lang.IllegalArgumentException - if the private key contains an occurrence of the delimiter.
    • Method Detail

      • getPrivateKey

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

        public java.util.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​(java.lang.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 java.lang.String toString()
        Specified by:
        toString in interface Nonce
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of the nonce, suitable for serialization.