6

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?

2 Answers 2

6

Solved it.. Use BigInteger

val md5 = MessageDigest.getInstance("MD5")
val hash = BigInteger(1, md5.digest(queryToSign.toByteArray(Charset.defaultCharset()))).toString(16)
Sign up to request clarification or add additional context in comments.

2 Comments

Otherwise using Hex converters (apache codex for example) will do the trick
For everyone: although it is a pretty-looking solution, BE AWARE all leading zeros will be TRIMMED. In case of calculating hash it is critical. So there must also be a code that checks the result's length and completes it with zeros if needed. For example, val correctedHash = "0".repeat(bytes.size * 2 - hash.length) + hash
0

byteArrayOf(80,87,68).decodeToString()

= PWD

click here to see how it works

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.