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:

Check if value is present as key in a Map in Java

This example shows how to check if a value exists as a key in a map. Let’s say we have a ComboBox and we want to compare the selected value in the ComboBox with they keys in a map to see if it exists.

There are a few different options to do just that and the example below will show a few of them. Let’s assume we have a ComboBox with a corresponding model called comboBoxModel.

In the example above the method keySet is called on the coordinates to get a Collection (a Set) which has a method called contains where we just send in our value to look for. The method returns true if it exists, otherwise false.

In the example above we check if value exists as key in the Hashtable by using lambda expressions which is available from Java version 1.8.
We get the Stream from the keySet and filter out values that matches the selected value and call the findFirst() method. In this case it should only be one item that matches the value so we could also call the findAny() method. Both of those methods return an Optional object whether the value is found or not. Then call isPresent() on the object to check if the value is actually found. If so, call get() on it to retrieve the value.

If you don’t want to use an optional object you can instread call orElse() method and specify what value should be returned if not found. In the example above we just return null, and the if statement just do a null-check on the variable returned.

Read lines of text from a file with the BufferedReader class

This example shows how to read the contents of a file line by line.
By using the BufferedReader class we can read line-by-line without having to worry about
linebreaks. We just call it’s readLine() method for as long as it doesn’t return null, which it does when it has reached the end of the file.
Don’t forget to call close() on the BufferedReader when done.

Continue reading

Write to file using a BufferedWriter

When you want to output text to a file it’s better to use a Writer class instead of a OutputStream such as the BufferedOutputStream since the purpose of Writer classes are to handle textual content.
With the BufferedWriter (as opposed to the BufferedOutputStream) you don’t have to translate your String parameter to a byte array, and there is also a handy method for writing a new line character.
In this example we simply write two lines of text, and finally we call flush on the BufferedWriter object before closing it.

Continue reading