In software development, ensuring data integrity and verifying whether a file has changed is a common requirement. One effective way to achieve this is by using the CRC32 (Cyclic Redundancy Check) hash, which is a popular method for detecting changes in files. In this blog post, we will explore a Delphi function that calculates the CRC32 hash of a file, allowing you to detect any modifications easily.
Let's dive into the function:
function PACalculateCRC32FromFile(const FileName: string): string;
// Calculate FAST CRC32 File Hash (e.g. for the purpose of detecting if a file has changed)
var
IdCRC32: IdHashCRC.TIdHashCRC32; // Declare a variable to hold the CRC32 hash object
FileStream: System.Classes.TFileStream; // Declare a variable to hold the file stream
CRC32: Cardinal; // Declare a variable to hold the CRC32 value
begin
IdCRC32 := TIdHashCRC32.Create; // Create an instance of the CRC32 hash object
try
// The file specified by FileName is opened in read-only mode with shared deny-write access.
// This ensures that the file can be read while preventing other processes from writing to it:
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); // Open the file for reading
try
CRC32 := IdCRC32.HashValue(FileStream); // Calculate the CRC32 value of the file
// The computed CRC32 value, which is a Cardinal type, is converted to an 8-character hexadecimal string.
// This makes the result more readable and suitable for comparison purposes:
Result := IntToHex(CRC32, 8); // Convert the CRC32 value to a hexadecimal string
finally
FileStream.Free; // Ensure the file stream is freed even if an exception occurs
end;
finally
IdCRC32.Free; // Ensure the CRC32 hash object is freed even if an exception occurs
end;
end;
Click here to download a Delphi demo project that demonstrates and explains the functionality (including a compiled and signed executable):
https://drive.google.com/file/d/1xVPQgjP6DgtUsd2r65fJ5ocmRxckkc9A/view?usp=sharing
Understanding File Hashes - A Simple Explanation:
A file hash is like a unique digital fingerprint for a file. When you run a file through a special mathematical algorithm, it generates a short string of letters and numbers. This string, known as the hash, is unique to the content of that file.
Why Use File Hashes?
1. Verify Integrity: If you download a file from the internet, you can use a hash to make sure it hasn't been altered. By comparing the hash of the downloaded file with the hash provided by the source, you can confirm that the file is intact and unmodified.
2. Detect Changes: If you have a file and want to know if it has been changed, you can generate its hash and compare it with a previous hash. If the hashes are different, the file has been modified.
How It Works:
1. Algorithm: A mathematical formula (like CRC32, MD5, SHA-1) processes the file's content.
2. Output: The algorithm produces a fixed-length string (the hash), regardless of the file size.
Example:
• Original file: "Hello, World!"
• Hash: fc3ff98e8c6a0d3087d515c0473f8677
Even a tiny change in the file will produce a completely different hash:
• Modified file: "Hello, world!"
• Hash: 68e109f0f40ca72a15e05cc22786f8e6
This drastic change in the hash with even a small modification in the file content helps in easily detecting any alterations.
In summary, a file hash is a quick and reliable way to ensure a file's integrity and to detect any changes to its content.
No comments:
Post a Comment