The other day I was working on a new Android app which will help people in fighting every day spam and hoaxes we receive on our social media and messengers. For some reason, I wanted to get the checksum of an image or a video file. Hence, I immediately went onto Google and typed:
Calculate sha512 of an image on android
And…there were crickets! I found nothing which could help me out. I’ve solved my problem since then. So, I am writing this quick article for those of you who come here searching for a solution in future.
I am going to use Kotlin for this tutorial, but you can easily reverse-engineer the code to Java.
First of all you are require to get the raw data of your image file. Remember, we need access to the raw file and not the bitmap image via functions like
Once we have the image’s raw data, we can write a SHA-512 calculating function like this. You need to pass the raw bytes of your content to this function as an argument.
Finally, to print the output SHA-512 checksum we will need one more function like this one.
That’s it! If everything worked properly, you would have a valid checksum. You can use Ubuntu to verify your checksum using