Calculating MD5 hash in Java, Kotlin and Android

Carvia Tech | July 24, 2019 | | 1 views

MD5 is widely used hash function (cryptographically weak) that produces 128 bit hash value. It is mostly used as a checksum to verify data integrity, but only against unintentional corruption. It is not used for security purpose anymore because it suffer from extensive vulnerabilities(Collision and Preimage vulnerabilities specifically).

In this article we will discuss 3 different ways to compute MD5 (or theoretically any other Hash Function) using Java and Kotlin.

1. Java implementation

Java provides MessageDigest class that provides applications the functionality of a message digest algorithm, such as MD5, SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.

Using Java without external library
import javax.xml.bind.DatatypeConverter;

class Utils {

    public String md5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest();
        return DatatypeConverter.printHexBinary(digest).toUpperCase();

There are other ways also, for example using apache common codecs to compute the hash values, but we will not explore those options here.

2. Kotlin extension function

We can write extension functions in Kotlin that adds md5() as the function to String class.

Writing extension function in kotlin
import java.math.BigInteger

fun String.md5(): String {
    val md = MessageDigest.getInstance("MD5")
    return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0')

Now we can call this extension function to compute md5 on any arbitrary string value.

Using extension function
val input = "this is a dummy string"
val md5 = input.md5()
println("computed md5 value is $md5")

3. Normal Kotlin function

We can write a normal function in Kotlin that computes MD5 or any other hash using MessageDigest class.

import javax.xml.bind.DatatypeConverter

object Utils {

    fun sha1(input: String) = hashString("SHA-1", input)
    fun md5(input: String) = hashString("MD5", input)

    private fun hashString(type: String, input: String): String {
        val bytes = MessageDigest
        return DatatypeConverter.printHexBinary(bytes).toUpperCase()

The above class can be used to compute any type of hash supported by underlying JVM implementation like, SHA-1, SHA-256, MD5, etc.

Top articles in this category:
  1. Top 50 Multi-threading Java Interview Questions for Investment Bank
  2. Citibank Java developer interview questions
  3. Goldman Sachs Java Interview Questions for Senior Developer
  4. RBS Java Programming Interview Questions
  5. BlackRock Top Java Interview Questions: Investment Banking Domain
  6. ION Trading Java Interview Questions
  7. Cracking core java interviews - question bank

Find more on this topic:
Java Interviews image
Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info