Lua:md5file
| ⚠️ This page still needs significant improvements.
Please help by expanding or correcting this article where possible.
|
function md5file(pathToFile) : string
Calculates and returns the MD5 checksum of a file's contents. The result is returned as a lowercase hexadecimal string (32 hex characters). This is useful for integrity checks, detecting file changes, or verifying downloads.
Function Parameters
| Parameter | Type | Description |
|---|---|---|
| pathToFile | string | Path to the file to be hashed. Use an absolute or relative path. On Windows, backslashes (\) are allowed.
|
Returns
A string containing the MD5 hash of the file in lowercase hexadecimal (e.g. 9e107d9d372bb6826bd81d3542a419d6).
Description
The function opens the specified file, reads its bytes, computes the MD5 digest over the file contents, and returns the digest formatted as a 32-character hexadecimal string. If the file cannot be opened or read, the function may raise an error.
Usage Examples
-- Simple usage
local hash = md5file("C:\\Games\\mygame.exe")
print("MD5: " .. hash) -- e.g. MD5: 9e107d9d372bb6826bd81d3542a419d6
Script Error:Unable to open file "C:\path\to\file.bin"
Notes
- The function reads the raw bytes of the file; text encoding does not affect the hash.
- For very large files the operation can be slow and use memory depending on implementation.
- MD5 collisions are possible; do not use MD5 when collision resistance is required.
- When comparing MD5 strings, normalize case (lowercase) or use a case-insensitive comparison.