Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate an MD5 hash in Kotlin? [closed]

Generate MD5 hash of a string using standard library in Kotlin?

I have tried below mention code

import java.math.BigInteger
import java.security.MessageDigest

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

Is this the best way or which?

like image 545
Anisuzzaman Babla Avatar asked Oct 02 '20 12:10

Anisuzzaman Babla


People also ask

Can you reverse hash MD5?

No, it is not possible to reverse a hash function such as MD5: given the output hash value it is impossible to find the input message unless enough information about the input message is known.

What is MD5 generator?

An MD5 hash is generated by getting a string of any desired length and encoding it into a 128-bit fingerprint. Typing the same string using the MD5 generator will always produce the same 128-bit hash result.

What is MD5 method?

MD5 (message-digest algorithm) is a cryptographic protocol used for authenticating messages as well as content verification and digital signatures. MD5 is based on a hash function that verifies that a file you sent matches the file received by the person you sent it to.


2 Answers

In general, hash (digest) functions take a byte array as input and produce a byte array as an output. Therefore, to hash a string, you first need to convert it into a byte array. A common way of doing this is to encode the string as an array of UTF-8 bytes: string.toByteArray(UTF_8)

A common way to display a byte array as a string, is to convert the individual bytes to their hexadecimal values and concatenate them. Here is an extension function that does that:

fun ByteArray.toHex() = joinToString(separator = "") { byte -> "%02x".format(byte) }

MD5 produces a byte array of length 16. When converted to hex, it is represented by a string of length 32.

The entire code looks like this:

import java.security.MessageDigest
import kotlin.text.Charsets.UTF_8

fun md5(str: String): ByteArray = MessageDigest.getInstance("MD5").digest(str.toByteArray(UTF_8))
fun ByteArray.toHex() = joinToString(separator = "") { byte -> "%02x".format(byte) }

fun main() {
    println(md5("Hello, world!").toHex()) //6cd3556deb0da54bca060b4c39479839
    println(md5("").toHex())              //d41d8cd98f00b204e9800998ecf8427e
}

Note that MD5 has well known weaknesses that make it inappropriate for many use cases. Alternatives include the SHA family of hashing functions. Here is how apply SHA-256 on a string:

fun sha256(str: String): ByteArray = MessageDigest.getInstance("SHA-256").digest(str.toByteArray(UTF_8))
like image 160
David Soroko Avatar answered Sep 18 '22 17:09

David Soroko


Using java.security.MessageDigest is the simplest way

import java.math.BigInteger
import java.security.MessageDigest

fun md5(input:String): String {
    val md = MessageDigest.getInstance("MD5")
    return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
}
like image 30
Anisuzzaman Babla Avatar answered Sep 21 '22 17:09

Anisuzzaman Babla