Engee documentation
Notebook

Calculating the CRC-32 checksum in Julia

Brief information

In this example, we use the Julia programming language and the Libz library to calculate the CRC-32 checksum from a string.

Introduction

What is CRC-32?

CRC-32* (Cyclic Redundancy Check, 32-bit) is a checksum calculation algorithm used to verify data integrity. It is used when transferring or storing data to ensure that the data has not been corrupted. The algorithm generates a 32-bit number (hash) unique to a specific sequence of bytes.
CRC-32 is widely used in network protocols, archivers (for example, ZIP), data storage systems and many other applications where fast information integrity verification is required.
In this example, we apply the CRC-32 algorithm to the string "The quick brown fox jumps over the lazy dog", converting it into a sequence of bytes and calculating the checksum.

The main part

Installing the required Libz package (if it is not already installed)

In [ ]:
import Pkg; Pkg.add("Libz")

We connect the Libz library, which contains the CRC-32 implementation.

In [ ]:
using Libz

Create the string "The quick brown fox jumps over the lazy dog" and convert it to a byte array. UInt8

In [ ]:
data = UInt8.(b"The quick brown fox jumps over the lazy dog")
Out[0]:
43-element Vector{UInt8}:
 0x54
 0x68
 0x65
 0x20
 0x71
 0x75
 0x69
 0x63
 0x6b
 0x20
 0x62
 0x72
 0x6f
    ⋮
 0x74
 0x68
 0x65
 0x20
 0x6c
 0x61
 0x7a
 0x79
 0x20
 0x64
 0x6f
 0x67

Calculating the CRC-32 hash from an array of bytes: the Libz.crc32 function takes an array of bytes and returns a 32-bit integer

In [ ]:
crc_value = Libz.crc32(data)
Out[0]:
0x414fa339

Converting the result to a hexadecimal string without a prefix 0x and we output the result

In [ ]:
hex_string = string(crc_value, base=16)
println(hex_string)
414fa339

The final result

For the string "The quick brown fox jumps over the lazy dog", we get the CRC-32 value in hexadecimal form: 414fa339.

This value may vary depending on the CRC-32 standard used (for example, IEEE 802.3, reversed, etc.). In this case, an implementation compatible with the widespread version is used.

Conclusion

We looked at how, using the Julia language and the Libz library, you can easily and quickly calculate the CRC-32 checksum value for any byte sequence. Such calculations are useful for verifying the integrity of data during transmission or storage. In the example above, the string is converted to bytes, after which the CRC-32 algorithm is applied to it, and the result is output in convenient hexadecimal format.

The example was developed using materials from Rosetta Code