java.lang.Object
com.renomad.minum.utils.StringUtils
Some helper methods for Strings.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringbyteArrayToString(byte[] byteArray) Converts an array of bytes to a string.static StringbyteListToString(List<Byte> byteList) Converts a list of bytes to a string.static StringDecodes URL-encoded UTF-8 text, except that we first check if the string value is the token %NULL%, which is our way to signify null.static StringEncodes UTF-8 text using URL-encodingstatic StringgenerateSecureRandomString(int length) Returns a string of random characters.static booleanisPureDigits(String str) A helper method to assess whether the input consists of only digits.static StringReplace dangerous text that would go inside an HTML attribute.static StringReturns text that has three symbols replaced - the less-than, greater-than, and ampersand.
-
Method Details
-
safeHtml
Returns text that has three symbols replaced - the less-than, greater-than, and ampersand. See ...
If the text is going inside an attribute (e.g.This will protect against something like <div>$USERNAME</div> allowing a username of <script>alert(1)</script> becoming <div><script>alert(1)</script</div> and instead becomes <div><script>alert(1)</script></div><div class="TEXT_GOES_HERE">) Then you need to escape slightly differently. In that case see [safeAttr] -
safeAttr
Replace dangerous text that would go inside an HTML attribute. SeesafeHtml(String)
If we get a null string, just return an empty string
Note: When cleaning data which will be used in an HTML attribute, ensure that the output of this method will be placed inside single or double quotes.
Valid use: the key word HERE in the following:<foo class="HERE">or<foo class='HERE'>
Invalid use:<foo class=HERE>example: Given alert('XSS Attack') Get alert('XSS Attack') -
encode
Encodes UTF-8 text using URL-encoding -
decode
Decodes URL-encoded UTF-8 text, except that we first check if the string value is the token %NULL%, which is our way to signify null. -
generateSecureRandomString
Returns a string of random characters.- Parameters:
length- a positive non-zero number less than 1000
-
byteListToString
Converts a list of bytes to a string. Returns null if the input is null. -
byteArrayToString
Converts an array of bytes to a string. Returns null if the input is null. -
isPureDigits
A helper method to assess whether the input consists of only digits. This helps us in the case of checking the content-length header, since it must be pure numbers, no "-" or "+" symbols preceding it.- Returns:
- true if the given string consists exclusively of digits, like "1234", false if anything else, like "+1234" or "12.34" or "" or null.
-