Compute SHA256 Hash in Android/Java and C# Compute SHA256 Hash in Android/Java and C# asp.net asp.net

Compute SHA256 Hash in Android/Java and C#


Your Java code is wrong: you are adding the input bytes twice. If you are calculating this in one go, you need to either call only digest(bytes) or call digest() after update(bytes);


I was looking for a Kotlin version for my Android app.

I could not find one; here is what I came up with:

fun String.getSha256(): String {    val digest = MessageDigest.getInstance("SHA-256").apply { reset() }    val byteData: ByteArray = digest.digest(this.toByteArray())    return StringBuffer().apply {        byteData.forEach {            append(((it.toInt() and 0xff) + 0x100).toString(16).substring(1))        }    }.toString()}