Class EncodingUtils


  • public class EncodingUtils
    extends java.lang.Object
    This is EncodingUtils that encapsulates common base64, signing and encryption calls and operations in one spot.
    Since:
    5.0.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String JSON_WEB_KEY
      JSON web key parameter that identifies the key..
    • Constructor Summary

      Constructors 
      Constructor Description
      EncodingUtils()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decodeBase64​(byte[] data)
      Base64-decode the given string as byte[].
      static byte[] decodeBase64​(java.lang.String data)
      Base64-decode the given string as byte[].
      static java.lang.String decodeBase64ToString​(java.lang.String data)
      Base64-decode the given string as String.
      static byte[] decodeUrlSafeBase64​(java.lang.String data)
      Base64-decode the given string as byte[].
      static java.lang.String decryptJwtValue​(java.security.Key secretKeyEncryptionKey, java.lang.String value)
      Decrypt value based on the key created.
      static java.lang.String encodeBase32​(byte[] data, boolean chunked)
      Base32-encode the given byte[] as a string.
      static java.lang.String encodeBase64​(byte[] data)
      Base64-encode the given byte[] as a string.
      static java.lang.String encodeBase64​(byte[] data, boolean chunked)
      Base64-encode the given byte[] as a string.
      static java.lang.String encodeBase64​(java.lang.String data)
      Base64-encode the given string as a string.
      static byte[] encodeBase64ToByteArray​(byte[] data)
      Base64-encode the given byte[] as a byte[].
      static java.lang.String encodeUrlSafeBase64​(byte[] data)
      Base64-encode the given byte[] as a string.
      static java.lang.String encryptValueAsJwtDirectAes128Sha256​(java.security.Key key, java.io.Serializable value)
      Encrypt value as jwt with direct algorithm and encryption content alg aes-128-sha-256.
      static java.lang.String encryptValueAsJwtRsaOeap256Aes256Sha512​(java.security.Key key, java.io.Serializable value)
      Encrypt value as jwt rsa oeap 256 aes 256 sha 512 string.
      static java.lang.String generateJsonWebKey​(int size)
      Generate octet json web key of given size .
      static java.security.Key generateJsonWebKey​(java.lang.String secret)
      Prepare json web token key.
      static java.security.Key generateJsonWebKey​(java.util.Map<java.lang.String,​java.lang.Object> params)
      Generate json web key.
      static java.lang.String hexDecode​(char[] data)
      Hex decode string.
      static java.lang.String hexDecode​(java.lang.String data)
      Hex decode string.
      static java.lang.String hexEncode​(byte[] data)
      Hex encode string.
      static java.lang.String hexEncode​(java.lang.String data)
      Hex encode string.
      static boolean isBase64​(java.lang.String value)
      Validates Base64 encoding.
      boolean isJsonWebKey​(java.lang.String key)
      Is json web key boolean.
      static org.jose4j.jwk.JsonWebKey newJsonWebKey​(int size)
      New Json web key.
      static org.jose4j.jwk.JsonWebKey newJsonWebKey​(java.lang.String json)
      New Json web key.
      static java.util.Map<java.lang.String,​java.lang.Object> parseJsonWebKey​(java.lang.String key)
      Parse json web key map.
      static byte[] signJwsHMACSha256​(java.security.Key key, byte[] value, java.util.Map<java.lang.String,​java.lang.Object> headers)
      Sign jws hmac sha 256.
      static byte[] signJwsHMACSha512​(java.security.Key key, byte[] value, java.util.Map<java.lang.String,​java.lang.Object> headers)
      Sign jws.
      static byte[] signJwsRSASha512​(java.security.Key key, byte[] value, java.util.Map<java.lang.String,​java.lang.Object> headers)
      Sign jws.
      static java.lang.String urlDecode​(java.lang.String value)
      Url decode a value.
      static java.lang.String urlEncode​(java.lang.String value)
      Url encode a value via UTF-8.
      static java.lang.String urlEncode​(java.lang.String value, java.lang.String encoding)
      Url encode a value.
      static byte[] verifyJwsSignature​(java.security.Key signingKey, byte[] value)
      Verify jws signature byte [ ].
      static byte[] verifyJwsSignature​(java.security.Key signingKey, java.lang.String asString)
      Verify jws signature byte [ ].
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JSON_WEB_KEY

        public static final java.lang.String JSON_WEB_KEY
        JSON web key parameter that identifies the key..
        See Also:
        Constant Field Values
    • Constructor Detail

      • EncodingUtils

        public EncodingUtils()
    • Method Detail

      • hexDecode

        public static java.lang.String hexDecode​(java.lang.String data)
        Hex decode string.
        Parameters:
        data - the data
        Returns:
        the string
      • hexDecode

        public static java.lang.String hexDecode​(char[] data)
        Hex decode string.
        Parameters:
        data - the data
        Returns:
        the string
      • hexEncode

        public static java.lang.String hexEncode​(java.lang.String data)
        Hex encode string.
        Parameters:
        data - the data
        Returns:
        the string
      • hexEncode

        public static java.lang.String hexEncode​(byte[] data)
        Hex encode string.
        Parameters:
        data - the data
        Returns:
        the string
      • encodeUrlSafeBase64

        public static java.lang.String encodeUrlSafeBase64​(byte[] data)
        Base64-encode the given byte[] as a string.
        Parameters:
        data - the byte array to encode
        Returns:
        the encoded string
      • decodeUrlSafeBase64

        public static byte[] decodeUrlSafeBase64​(java.lang.String data)
        Base64-decode the given string as byte[].
        Parameters:
        data - the base64 string
        Returns:
        the encoded array
      • encodeBase64

        public static java.lang.String encodeBase64​(byte[] data)
        Base64-encode the given byte[] as a string.
        Parameters:
        data - the byte array to encode
        Returns:
        the encoded string
      • encodeBase64

        public static java.lang.String encodeBase64​(byte[] data,
                                                    boolean chunked)
        Base64-encode the given byte[] as a string.
        Parameters:
        data - the byte array to encode
        chunked - the chunked
        Returns:
        the encoded string
      • encodeBase64

        public static java.lang.String encodeBase64​(java.lang.String data)
        Base64-encode the given string as a string.
        Parameters:
        data - the String to encode
        Returns:
        the encoded string
      • decodeBase64

        public static byte[] decodeBase64​(java.lang.String data)
        Base64-decode the given string as byte[].
        Parameters:
        data - the base64 string
        Returns:
        the decoded array
      • decodeBase64

        public static byte[] decodeBase64​(byte[] data)
        Base64-decode the given string as byte[].
        Parameters:
        data - the base64 string
        Returns:
        the decoded array
      • decodeBase64ToString

        public static java.lang.String decodeBase64ToString​(java.lang.String data)
        Base64-decode the given string as String.
        Parameters:
        data - the base64 string
        Returns:
        the string
      • encodeBase64ToByteArray

        public static byte[] encodeBase64ToByteArray​(byte[] data)
        Base64-encode the given byte[] as a byte[].
        Parameters:
        data - the byte array to encode
        Returns:
        the byte[] in base64
      • encodeBase32

        public static java.lang.String encodeBase32​(byte[] data,
                                                    boolean chunked)
        Base32-encode the given byte[] as a string.
        Parameters:
        data - the byte array to encode
        chunked - the chunked
        Returns:
        the encoded string
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String value)
        Url encode a value via UTF-8.
        Parameters:
        value - the value to encode
        Returns:
        the encoded value
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String value,
                                                 java.lang.String encoding)
        Url encode a value.
        Parameters:
        value - the value to encode
        encoding - the encoding
        Returns:
        the encoded value
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String value)
        Url decode a value.
        Parameters:
        value - the value to decode
        Returns:
        the decoded value
      • isBase64

        public static boolean isBase64​(java.lang.String value)
        Validates Base64 encoding.
        Parameters:
        value - the value to check
        Returns:
        true if the string is validly Base64 encoded
      • verifyJwsSignature

        public static byte[] verifyJwsSignature​(java.security.Key signingKey,
                                                java.lang.String asString)
        Verify jws signature byte [ ].
        Parameters:
        signingKey - the signing key
        asString - the as string
        Returns:
        the byte [ ]
      • verifyJwsSignature

        public static byte[] verifyJwsSignature​(java.security.Key signingKey,
                                                byte[] value)
        Verify jws signature byte [ ].
        Parameters:
        value - the value
        signingKey - the signing key
        Returns:
        the byte [ ]
      • isJsonWebKey

        public boolean isJsonWebKey​(java.lang.String key)
        Is json web key boolean.
        Parameters:
        key - the key
        Returns:
        true/false
      • newJsonWebKey

        public static org.jose4j.jwk.JsonWebKey newJsonWebKey​(java.lang.String json)
        New Json web key.
        Parameters:
        json - the json
        Returns:
        the json web key
      • newJsonWebKey

        public static org.jose4j.jwk.JsonWebKey newJsonWebKey​(int size)
        New Json web key.
        Parameters:
        size - the size
        Returns:
        the json web key
      • generateJsonWebKey

        public static java.lang.String generateJsonWebKey​(int size)
        Generate octet json web key of given size .
        Parameters:
        size - the size
        Returns:
        the key
      • generateJsonWebKey

        public static java.security.Key generateJsonWebKey​(java.lang.String secret)
        Prepare json web token key.
        Parameters:
        secret - the secret
        Returns:
        the key
      • generateJsonWebKey

        public static java.security.Key generateJsonWebKey​(java.util.Map<java.lang.String,​java.lang.Object> params)
        Generate json web key.
        Parameters:
        params - the params
        Returns:
        the key
      • signJwsHMACSha512

        public static byte[] signJwsHMACSha512​(java.security.Key key,
                                               byte[] value,
                                               java.util.Map<java.lang.String,​java.lang.Object> headers)
        Sign jws.
        Parameters:
        key - the key
        value - the value
        headers - the headers
        Returns:
        the byte []
      • signJwsHMACSha256

        public static byte[] signJwsHMACSha256​(java.security.Key key,
                                               byte[] value,
                                               java.util.Map<java.lang.String,​java.lang.Object> headers)
        Sign jws hmac sha 256.
        Parameters:
        key - the key
        value - the value
        headers - the headers
        Returns:
        the byte [ ]
      • signJwsRSASha512

        public static byte[] signJwsRSASha512​(java.security.Key key,
                                              byte[] value,
                                              java.util.Map<java.lang.String,​java.lang.Object> headers)
        Sign jws.
        Parameters:
        key - the key
        value - the value
        headers - the headers
        Returns:
        the byte []
      • encryptValueAsJwtDirectAes128Sha256

        public static java.lang.String encryptValueAsJwtDirectAes128Sha256​(java.security.Key key,
                                                                           java.io.Serializable value)
        Encrypt value as jwt with direct algorithm and encryption content alg aes-128-sha-256.
        Parameters:
        key - the key
        value - the value
        Returns:
        the string
      • encryptValueAsJwtRsaOeap256Aes256Sha512

        public static java.lang.String encryptValueAsJwtRsaOeap256Aes256Sha512​(java.security.Key key,
                                                                               java.io.Serializable value)
        Encrypt value as jwt rsa oeap 256 aes 256 sha 512 string.
        Parameters:
        key - the key
        value - the value
        Returns:
        the string
      • decryptJwtValue

        public static java.lang.String decryptJwtValue​(java.security.Key secretKeyEncryptionKey,
                                                       java.lang.String value)
        Decrypt value based on the key created.
        Parameters:
        secretKeyEncryptionKey - the secret key encryption key
        value - the value
        Returns:
        the decrypted value
      • parseJsonWebKey

        public static java.util.Map<java.lang.String,​java.lang.Object> parseJsonWebKey​(java.lang.String key)
                                                                                      throws java.lang.Exception
        Parse json web key map.
        Parameters:
        key - the key
        Returns:
        the map
        Throws:
        java.lang.Exception - the exception