JBStrap Utils - JSON, Encryption

CryptoUtils

Use this class to implement basic encryption to text values (connection and password encryption). The class supports the following encryption types:

MD5

This cryptographic algorithm is a hash function, that was designed to be undecryptable. It is most frequently used to store passwords, as safe encryption is ensured. To check the password, the password entered by the user must be encrypted. If two encrypted passwords match, the password provided by the user can be accepted.

As a result of the encryption, a text value is returned, containing the encrypted value in hexadecimal format.

Example of encrypting the text “ExampleText” using the MD5 algorithm:

Learn more about the MD5 algorithm here .

AES 128bit

This is a decryptable algorithm. The encrypted text can be used for communication or to store data later to be decrypted to retrieve the original values. To use this algorithm, you need a 128-bit secure key ad a 128-bit init vector. If either one of them is unknown or the value does not match the one provided for encryption, the decryption will fail.

Example of encrypting the text “ExampleText”:

As a result of the encryption, a text value will be returned, containing the encrypted value in hexadecimal format. Such values can be decrypted if the key and the vector are available.

Example of data decryption:

As a result of decryption, the “ExampleText” value will be returned.

To learn more about AES128bit, click here .

Base64

Base64 is not an encryption but a way of encoding. You do not need any sort of key, only the use of the algorithm. If a value is encoded using Base64, you may decode it using any other algorithm containing Base64. Base64 is most frequently used to display an image on the screen. Browsers support displaying images as Base64-encoded information.

Example of encoding the text “ExampleText” using Base64:
Example of decrypting a value:

To learn more about the Base64 encoding, click here .

HTMLUtils

This class supports HTML content handling. Its first basic functionality is to generate HTML code for a component. Use it to insert a component into an HTML-based email. The second important functionality encoding a text to be displayed in HTML. Use it if you want to display a text which contains characters that are reserved in HTML (e.g. ">" and "<").

Example of generating HTML code for a component. The generated HTML code will include the subcomponents:
Example of escaping a text with reserved characters:

JSONUtil

By using this class, we can convert any POJO object into a JSON string. The resulting string can be used as a JSON object.

Example of a POJO class:
Converting a POJO class into a JSON string:

After running the above code, the following JSON object will be generated as a JSON String:

StringUtils

Using this class you can easily perform complex string operations, such as cutting out a section from a text value following a pattern or using delimiters to separate texts.

Suppose we have an XML content that is available in text form. We want to extract the value of a tag from this content. All you have to do is call the subString method with the appropriate RegEx expression.

Extracting a value from an XML source stored as text:

When running the example above, the value of the myTagValue variable will be equal to "value", since this text was included in the xmlData variable between the myTag tags.

Consider another common case where we have a list in text format from which we want to extract a specified item. This can also be easily done using the getToken method.

Determining an item in a list stored in text format:

When running the above code, the variable secondElement takes the value "2" as it is the second item in our list. In the example, we used "," as a separator character, but in reality, it could be any character or text.

CriteriaUtils

By using the class, you can easily perform common operations on Criteria objects. Such operations may include generating the Criteria class from text data (for example, a condition saved in a database), or negating a condition or concatenating one or more conditions.

Generating a Criteria class from text data:

After running the above code, the criteria variable will contain a Criteria object that matches the condition built in the following code snippet:

that is, a condition that satisfies any record in which the value of the column named valid is 'Y'.

Code snippet negating a condition:

After the code is executed, the value of the negated C riteria will be the negated form of the original condition, so any record in which the value column value is not "Y" will match the negated C riteria .

Concatenating two criteria:

After running the above code, the combined criteria will contain both conditions concatenated with an AND logical relationship. If any parameter of the getCombinedCriteria method receives a null value, the returned condition returned will contain only the non-null condition. If both parameters are null, then the return value will be zero.

Related pages: