Package org.forgerock.json.jose.utils
Class Utils
java.lang.Object
org.forgerock.json.jose.utils.Utils
This class provides utility methods to share common behaviour.
- Since:
- 2.0.0
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Base64url decodes the given String and converts the decoded bytes into a UTF-8 String.static String
Base64url encodes the given String, converting the String to UTF-8 bytes.static boolean
constantEquals
(byte[] a, byte[] b) Compares two byte arrays for equality, in a constant time.Parses the given JSON string into a NoDuplicatesMap.static String
writeJsonObject
(Map<String, Object> object) Writes the given map as a string in JSON object format.
-
Field Details
-
CHARSET
UTF-8 Charset.
-
-
Method Details
-
base64urlEncode
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
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
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
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
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.
-