Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

557
Visualizações
How to Generate an MD5 hash in Kotlin?

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?

over 4 years ago · Hanz Gallego
4 Respostas
Responde à pergunta

0

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')
}
over 4 years ago · Hanz Gallego Relatório

0

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

fun main(args: Array<String>) {
    println(md5Hash("Hello, world!"))
}

fun md5Hash(str: String): String {
    val md = MessageDigest.getInstance("MD5")
    val bigInt = BigInteger(1, md.digest(str.toByteArray(Charsets.UTF_8)))
    return String.format("%032x", bigInt)
}
over 4 years ago · Hanz Gallego Relatório

0

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))
over 4 years ago · Hanz Gallego Relatório

0

I would suggest utilsing version 1.15 of Apache commons codec

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>
import org.apache.commons.codec.digest.DigestUtils

fun main(args: Array<String>) {
    println(DigestUtils.md5Hex("Hello world!"))
}
over 4 years ago · Hanz Gallego Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda