Generating SHA-512 checksum of a file in Android

Rajat Saxena
Jun 24, 2018 · 2 min read

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.

Lets begin.

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 Mediastore.mages.Media.getBitmap.

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 sha512sum command.

Follow Rayn Studios official medium blog for more.

Consider subscribing to my YouTube channel as well where I publish weekly tech related videos. Geek stuff mostly.

Rayn Studios

App Development & Coaching

Rajat Saxena

Written by

Developer, Entrepreneur & Founder of Rayn Studios

Rayn Studios

App Development & Coaching

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade