0

How to calculate SHA-256 of a bitmap image in android studio?

I have already tried converting image into byte array and then find its message digest but it gives a completely different answer than what I find using online SHA-256 converters. I know how to calculate SHA-256 for a string but I am unable to calculate SHA-256 for a bitmap image.

Nirojan Selvanathan
  • 10,066
  • 5
  • 61
  • 82
Thatz'it
  • 11
  • 2
  • Please share your code which you have tried – Romil Patel Jun 24 '19 at 04:20
  • 3
    What have you tried so far? Please [edit] your question to include a [mcve] of your code, including any implementations you might have already (for example, calculating a SHA256 from a string), and anything you've tried so far to get this working yourself. – Hoppeduppeanut Jun 24 '19 at 04:21

1 Answers1

0

Try this

fun sampleHashFile(bitmap: Bitmap): String {
    val baos = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos)
    val bb = baos.toByteArray()

    val bc = MessageDigest.getInstance("SHA-256").digest(bb)
    return BigInteger(1, bc).toString(16)
}**strong text**
ylinkz
  • 181
  • 1
  • 2
  • 13