Class Utils

java.lang.Object
org.forgerock.json.jose.utils.Utils

public final class Utils extends Object
This class provides utility methods to share common behaviour.
Since:
2.0.0
  • Field Details

  • Method Details

    • base64urlEncode

      public static String base64urlEncode(String s)
      Base64url encodes the given String, converting the String to UTF-8 bytes.
      Parameters:
      s - The String to encoded.
      Returns:
      A Base64url encoded UTF-8 String.
    • base64urlDecode

      public static String base64urlDecode(String s)
      Base64url decodes the given String and converts the decoded bytes into a UTF-8 String.
      Parameters:
      s - The Base64url encoded String to decode.
      Returns:
      The UTF-8 decoded String.
    • constantEquals

      public static boolean constantEquals(byte[] a, byte[] b)
      Compares two byte arrays for equality, in a constant time.

      If the two byte arrays don't match the method will not return until the whole byte array has been checked. This prevents timing attacks. Unless the two arrays are not off equal length, and in this case the method will return immediately.

      Parameters:
      a - One of the byte arrays to compare.
      b - The other byte array to compare.
      Returns:
      true if the arrays are equal, false otherwise.
    • parseJson

      public static Map<String,Object> parseJson(String json)
      Parses the given JSON string into a NoDuplicatesMap.

      The JWT specification details that any JWT with duplicate header parameters or claims MUST be rejected so a Map implementation is used to parse the JSON which will throw an exception if an entry with the same key is added to the map more than once.

      Parameters:
      json - The JSON string to parse.
      Returns:
      A Map of the JSON properties.
      Throws:
      InvalidJwtException - if the json value is not well formed or contains duplicate keys.
    • writeJsonObject

      public static String writeJsonObject(Map<String,Object> object)
      Writes the given map as a string in JSON object format.
      Parameters:
      object - the object to write as JSON.
      Returns:
      the JSON serialisation of the given object.
      Throws:
      InvalidJwtException - if the object cannot be converted to JSON for any reason.