Class Disclosure


  • public class Disclosure
    extends Object
    A class that represents the "Disclosure" defined in the SD-JWT specification.

    Instances of this class are immutable.

    Example 1:

     // Parameters for the constructor.
     String salt       = "_26bc4LT-ac6q2KI6cBW5es";
     String claimName  = "family_name";
     Object claimValue = "Möbius";
    
     // Create a Disclosure instance with the parameters.
     Disclosure disclosure =
         new Disclosure(salt, claimName, claimValue);
    
     // Get the string representation of the disclosure.
     // disclosure.toString() returns the same result.
     String dc = disclosure.getDisclosure();
    
     // dc -> "WyJfMjZiYzRMVC1hYzZxMktJNmNCVzVlcyIsImZhbWlseV9uYW1lIiwiTcO2Yml1cyJd"
     

    Example 2:

     // Parse a string representation of disclosure.
     Disclosure disclosure = Disclosure.parse(
         "WyI2cU1RdlJMNWhhaiIsICJmYW1pbHlfbmFtZSIsICJNw7ZiaXVzIl0");
    
     // Compute the digest of the disclosure with the default
     // hash algorithm ("sha-256"). disclosure.digest("sha-256")
     // returns the same result.
     String digest = disclosure().digest();
    
     // digest -> "uutlBuYeMDyjLLTpf6Jxi7yNkEF35jdyWMn9U7b_RYY"
     

    Example 3:

     // Parameters for the constructor.
     String salt       = "_26bc4LT-ac6q2KI6cBW5es";
     String claimName  = "my_array";
     int    claimIndex = 0;
     Object claimValue = "my_array_element_at_index_0";
    
     // Create a Disclosure instance representing an array element.
     Disclosure disclosure =
         new Disclosure(salt, claimName, claimIndex, claimValue);
     
    Since:
    1.0
    See Also:
    Selective Disclosure for JWTs (SD-JWT)
    • Constructor Detail

      • Disclosure

        public Disclosure​(String claimName,
                          Object claimValue)
        Constructor with a pair of claim name and claim value. A salt is randomly generated.
        Parameters:
        claimName - A claim name. Must not be null.
        claimValue - A claim value. May be null.
        Throws:
        IllegalArgumentException - The given claim name is null.
      • Disclosure

        public Disclosure​(String salt,
                          String claimName,
                          Object claimValue)
        Constructor with a salt and a pair of claim name and claim value.
        Parameters:
        salt - A salt. Must not be null. It is recommended that the salt have 128-bit or higher entropy and be base64url-encoded.
        claimName - A claim name. Must not be null.
        claimValue - A claim value. May be null.
        Throws:
        IllegalArgumentException - The given salt and/or claim name are null.
      • Disclosure

        public Disclosure​(String claimName,
                          int claimIndex,
                          Object claimValue)
        Constructor with a claim name (an array name), a claim index (an array index) and a claim value. A salt is randomly generated.

        The purpose of this constructor is to create a disclosure for an array element.

        Parameters:
        claimName - A claim name (an array name). Must not be null.
        claimIndex - A claim index (an array index). For an array element, 0 or a positive integer should be given. If a negative integer is given, the disclosure created by this constructor will not represent an array element.
        claimValue - A claim value. May be null.
        Throws:
        IllegalArgumentException - The given claim name is null.
        Since:
        1.1
      • Disclosure

        public Disclosure​(String salt,
                          String claimName,
                          int claimIndex,
                          Object claimValue)
        Constructor with a salt, a claim name (an array name), a claim index (an array index) and a claim value.
        Parameters:
        salt - A salt. Must not be null. It is recommended that the salt have 128-bit or higher entropy and be base64url-encoded.
        claimName - A claim name (an array name). Must not be null.
        claimIndex - A claim index (an array index). For an array element, 0 or a positive integer should be given. If a negative integer is given, the disclosure created by this constructor will not represent an array element.
        claimValue - A claim value. May be null.
        Throws:
        IllegalArgumentException - The given salt and/or claim name are null.
        Since:
        1.1
    • Method Detail

      • getSalt

        public String getSalt()
        Get the salt.
        Returns:
        The salt.
      • getClaimName

        public String getClaimName()
        Get the claim name.
        Returns:
        The claim name.
      • getClaimIndex

        public int getClaimIndex()
        Get the claim index.

        When this disclosure represents an array element, this method returns 0 or a positive integer. Otherwise, this method returns a negative integer.

        Returns:
        The claim index.
        Since:
        1.1
      • getClaimValue

        public Object getClaimValue()
        Get the claim value.
        Returns:
        The claim value.
      • getJson

        public String getJson()
        Get the JSON representation of this disclosure. It is a JSON array with three elements. They are the salt, the claim name and the claim value.
        Returns:
        The JSON representation of this disclosure.
      • getDisclosure

        public String getDisclosure()
        Get the disclosure as a string.
        Returns:
        The disclosure as a string.
      • digest

        public String digest()
        Get the base64url-encoded digest of this disclosure computed with the default hash algorithm ("sha-256").

        The digest value with the default hash algorithm is computed on instance creation and the result is cached. This digest() method always returns the cached value. Therefore, calling this method is lightweight.

        Returns:
        The base64url-encoded digest of this disclosure computed with the default hash algorithm ("sha-256").
      • digest

        public String digest​(String hashAlgorithm)
        Get the base64url-encoded digest of this disclosure computed with the specified hash algorithm.
        Parameters:
        hashAlgorithm - A hash algorithm. Must not be null. If the given hash algorithm is equal to the default hash algorithm ("sha-256"), the cached digest value is returned (cf. digest()).
        Returns:
        The base64url-encoded digest of this disclosure computed with the specified hash algorithm.
        See Also:
        IANA: Named Information Hash Algorithm Registry
      • toString

        public String toString()
        Get the string representation of this instance is returned, which is the disclosure in the base64url format.
        Overrides:
        toString in class Object
        Returns:
        The disclosure in the base64url format.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • parse

        public static Disclosure parse​(String disclosure)
        Parse the given string as a disclosure.
        Parameters:
        disclosure - A string representation of disclosure. If null is given, null is returned.
        Returns:
        A Disclosure instance created as a result of parsing the input string.
        Throws:
        IllegalArgumentException - (1) The given string is not base64url-encoded. (2) The base64url-decoded value of the given string is not a valid UTF-8 byte sequence. (3) The JSON that the given string represents fails to be parsed as a JSON array. (4) The size of the JSON array is not 3. (5) The first element of the JSON array is not a JSON string. (6) The second element of the JSON array is neither a JSON string nor a JSON array. (7) When the second element is a JSON array, (a) its size is not 2, (b) its first element is not a string, (c) its second element is not a number. (8) The claim name is a key reserved by the SD-JWT specification.