Skip to main content

How to calculate the checksum of a file in Kotlin

How to calculate the checksum of a file in Kotlin.

Here's a detailed step-by-step tutorial on how to calculate the checksum of a file in Kotlin.

Step 1: Import the necessary packages

To calculate the checksum of a file in Kotlin, we need to use the java.util.zip package. So, start by importing the necessary packages:

import java.io.FileInputStream
import java.util.zip.CRC32
import java.util.zip.Adler32
import java.security.MessageDigest

In this tutorial, we will cover three different types of checksums: CRC32, Adler32, and MD5.

Step 2: Read the file

Next, we need to read the file for which we want to calculate the checksum. We can use the FileInputStream class from the java.io package to read the file byte by byte.

fun readFile(filename: String): ByteArray {
val file = FileInputStream(filename)
val buffer = ByteArray(file.available())
file.read(buffer)
file.close()
return buffer
}

The readFile function takes the filename as a parameter and returns the contents of the file as a byte array.

Step 3: Calculate CRC32 checksum

To calculate the CRC32 checksum, we can use the CRC32 class from the java.util.zip package. Here's how you can calculate the CRC32 checksum of a file:

fun calculateCRC32(filename: String): Long {
val buffer = readFile(filename)
val crc32 = CRC32()
crc32.update(buffer)
return crc32.value
}

The calculateCRC32 function takes the filename as a parameter, reads the file using the readFile function, and then calculates the CRC32 checksum using the CRC32 class.

Step 4: Calculate Adler32 checksum

To calculate the Adler32 checksum, we can use the Adler32 class from the java.util.zip package. Here's how you can calculate the Adler32 checksum of a file:

fun calculateAdler32(filename: String): Long {
val buffer = readFile(filename)
val adler32 = Adler32()
adler32.update(buffer)
return adler32.value
}

The calculateAdler32 function is similar to the calculateCRC32 function but uses the Adler32 class to calculate the checksum.

Step 5: Calculate MD5 checksum

To calculate the MD5 checksum, we can use the MessageDigest class from the java.security package. Here's how you can calculate the MD5 checksum of a file:

fun calculateMD5(filename: String): String {
val buffer = readFile(filename)
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(buffer)
val sb = StringBuilder()
for (byte in digest) {
sb.append(String.format("%02x", byte))
}
return sb.toString()
}

The calculateMD5 function takes the filename as a parameter, reads the file using the readFile function, and then calculates the MD5 checksum using the MessageDigest class.

Step 6: Putting it all together

Now that we have all the necessary functions to calculate the checksums, we can put it all together in a main function:

fun main() {
val filename = "path/to/your/file.txt"

val crc32Checksum = calculateCRC32(filename)
println("CRC32 checksum: $crc32Checksum")

val adler32Checksum = calculateAdler32(filename)
println("Adler32 checksum: $adler32Checksum")

val md5Checksum = calculateMD5(filename)
println("MD5 checksum: $md5Checksum")
}

Replace "path/to/your/file.txt" with the actual path to your file. Running the main function will calculate and print the CRC32, Adler32, and MD5 checksums of the file.

That's it! You now know how to calculate the checksum of a file in Kotlin using CRC32, Adler32, and MD5 algorithms.