String to MD5 hash in Java

In this example we will create a MD5 hash from a String. This can be useful when you for example want to keep passwords secure in a database so no one can read them in plain text. The MD5 is a one-way hash so it’s not possible to restore the original value again. You must hash another value and compare with the previously hashed value to see if they match.

To hash a String with MD5 we get an instance of the MessageDigest class calling the getInstance() method and provide the String “MD5” as parameter.
Then we need to create an inputstream from the String we want to use and pass that inputstream to an instance of DigestInputStream – along with the MessageDigest instance. Once that is done we need to read the content of the stream and this is an important step. If we don’t read in the bytes from the inputstream the hashed value will always be the same. Since we don’t need to do anything explicit with the content read from the inputstream but only read it, we don’t supply a body for the while-loop.

Finally we call the digest() method of the MessageDigest instance which will return a byte array.

If we want to store our hashed password in a database column of type varchar (or the like) we need to convert the byte array to a String. In this case we will convert it to a hexadecimal String which is done in the main method when the call to stringToMD5 returns. Apache Commons codec library has a neat class called ‘Hex’ for doing that.
Then we print out our hexadecimal value, and the length of it. Try to hash different values and you’ll see that the length of the hash array will always be 16 and the length of the generated hex string will always be 32 characters long.

The output from the code above:

Convert a String to an InputStream in Java using Apache Commons IO

This code example shows how to convert a String to an InputStream using the Apache Commons IO library. Converting a String to an InputStream is sometimes necessary when for example we need to call some api that require a InputStream object as parameter but the value you need to pass is stored in a String object. To convert the parameter ‘name’ in the example below to an InputStream we simply use the IOUtils class which has a static method called ‘toInputStream’. The method takes the string we want to convert as parameter along with a character set and in the example we choose the common character set “UTF-8”.
Finally to make sure it has succeeded we print out every byte (character) in the stream on a separate row. Since the read-method of the inputstream returns an int, which is the decimal value of the byte, we convert the int value to a char before we print it out.

The output from the code above is:

Convert a String to an InputStream in Java

This code example shows how to convert a String to an InputStream. This is sometimes necessary when calling different api:s that require a InputStream object as parameter but the value you need to pass is stored in a String object. To convert the parameter ‘name’ in the example below to an InputStream we create a ByteArrayInputStream object which takes a byte array as parameter, so we simply call getBytes on the String we want to convert. To make sure it is in the proper format we also provide the character set UTF-8 to the getBytes method which we supply using a constant from the StandardCharsets class.
Finally to make sure it has succeeded we print out every byte (character) in the stream on a separate row. Since the read-method of the inputstream returns an int, which is the decimal value of the byte, we convert the int value to a char before we print it out.

The output from the code above will be:

Convert a String to int

The first and fastest way to convert a String to an integer value is to use the static method valueOf() of the Integer class. The valueOf() method can be used by other primitive wrapper classes too to convert between different types.

Another way is to use the static method parseInt of the Integer class to convert
a String to an int as below:

Continue reading

Compare two String using StringUtils from Apache Commons

The StringUtils class of the Apache Commons Lang library supplies a method for comparing String without checking them for null values first.
Normally when comparing with standard Java methods of the String class you need to check them for null first to avoid any NullPointerException being thrown, like:

However, with the StringUtils class you don’t need to do any null checks since it takes care of that part for you. The only thing you need to provide are the two strings to compare:

The output from the code above is:

How to remove characters from a String in Java

This example shows how to easily remove characters from a String using the StringUtils class in the Apache Commons Lang library.
The StringUtils class in the library has two overloaded methods for remove().
The first takes a char as parameter specifying what character to remove. All characters of that type is removed in the returned String.
The second overloaded version takes a sequence of characters (a String) as parameter along with the value to replace in.
The code below demonstrates this:

The output from the code above is:

How to reverse a string in Java

This example shows how to reverse a String using the StringUtils class in the Apache Commons Lang library. It also shows how to reverse parts of a String that are delimited by a certain delimiter.

The StringUtils class has one method for reversing a String, reverse(), and one method for reversing with delimiter, reverseDelimited(). The first one simply takes the String to be reversed as parameter, and the second one takes the string to be reversed as first parameter and the delimiter as the second parameter of type char. The example below show how to use them:

First we reverse String ‘s’ and ‘s1’ without any delimiters. Then we reverse ‘s1’ with delimiter. The whole String gets reversed. Last we reverse ‘s2’ with delimiter. Here only 1 and 4 gets reversed. The output from the code above is:

Left pad and right pad a String in Java using Apache Commons

Sometimes you might need a fixed length string but the current value is much shorter so you need to fill out the string to a certain length. This is where the StringUtils class of the Apache Commons Lang library comes in handy with its methods leftPad() and rightPad().

There are three overloaded versions of each:

leftPad(String str, int size)
leftPad(String str, int size, char padChar)
leftPad(String str, int size, String padStr)

The first one takes the string to be padded and a size for the new padded string. The returned string will be padded on the left hand side with whitespaces. If the size is smaller than the original string, the original string will be returned.

The second one takes a padChar as a third parameter which simply means it will use that char instead of whitespaces for padding.

The third one takes a sequence of characters as third parameter and will use that whole string when padding. If the sum of padding characters is not divisible with the number of characters in the sequence the character sequence will simply be chopped at the right end.

There are corresponding methods for rightPad, which do exactly the same but on the right hand side of the string.

rightPad(String str, int size)
rightPad(String str, int size, char padChar)
rightPad(String str, int size, String padStr)

Below are a few examples of the leftPad methods:

The output from the above code is:

How to join strings in Java using the Apache Commons StringUtils class

This example shows how to join values together into a String using the Apache Commons Lang library. The StringUtils class comes with many overloaded versions of the join() method and the example below shows a few of them.

The first join call is made by passing in a list of Integers and a dash-separator to the join method.

The second and third call is made by passing in an iterator and a separator. First by sending in an iterator from the keyset of a map, and the second by passing in an iterator from the values of the same map.

The last call is made by passing in an array of Objects and a null separator. The null seperator will just join the values together without anything in between.

The output from the code above looks like this: