I'm trying to generate MD5 of a string in my android code using kotlin..
val md5 = MessageDigest.getInstance("MD5")
val hash = md5.digest(queryToSign.toByteArray(Charset.defaultCharset())).toString()
But this gives me:
[B@118072
Any thoughts?
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.
To convert a string to byte array in Kotlin, use String. toByteArray() method. String. toByteArray() method returns a Byte Array created using the characters of the calling string.
To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.
Solved it.. Use BigInteger
val md5 = MessageDigest.getInstance("MD5")
val hash = BigInteger(1, md5.digest(queryToSign.toByteArray(Charset.defaultCharset()))).toString(16)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With