The hash module provides hash value calculation and UUID generation functions. It is a built-in module of xmake.
::: tip API
hash.md5(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() functionCalculates the MD5 hash value of the specified file or binary data and returns a hexadecimal format hash string.
Calculate file hash value:
-- Calculate MD5 hash value of a file
local checksum = hash.md5("/path/to/file.txt")
print("MD5: " .. checksum)
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate MD5 hash value of binary data
local data = bytes("hello")
local checksum = hash.md5(data)
print("MD5: " .. checksum)
Incorrect usage (don't do this):
-- ❌ Wrong: If "hello" is not a file path, it will incorrectly calculate the hash of the file path string
local checksum = hash.md5("hello")
-- ✅ Correct: Use bytes wrapper for binary data
local checksum = hash.md5(bytes("hello"))
::: tip API
hash.sha1(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() function
:::Calculates the SHA1 hash value of the specified file or binary data and returns a hexadecimal format hash string.
Calculate file hash value:
-- Calculate SHA1 hash value of a file
local checksum = hash.sha1("/path/to/file.txt")
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate SHA1 hash value of binary data
local checksum = hash.sha1(bytes("hello"))
::: tip API
hash.sha256(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() function
:::Calculates the SHA256 hash value of the specified file or binary data and returns a hexadecimal format hash string.
SHA256 is more secure than MD5 and is commonly used for package integrity verification:
Calculate file hash value:
-- Verify downloaded package file
local packagefile = "package.tar.gz"
local checksum = hash.sha256(packagefile)
if checksum ~= expected_hash then
raise("checksum mismatch!")
end
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate SHA256 hash value of binary data
local checksum = hash.sha256(bytes("hello"))
::: tip API
hash.uuid(name: <string>)
:::
| Parameter | Description |
|---|---|
| name | Name string for UUID generation |
local id = hash.uuid("name")
Generates a deterministic UUID based on the given name string. The same name always generates the same UUID.
Internally calls hash.uuid4(str), format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Suitable for generating fixed unique identifiers for specific configurations:
-- Generate deterministic IDs for different build configurations
local config_id = hash.uuid("debug-x64-windows")
::: tip API
hash.xxhash32(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() function
:::Calculates hash value using the xxHash32 algorithm. xxHash is an extremely fast non-cryptographic hash algorithm suitable for hash tables, checksums, and other scenarios.
Calculate file hash value:
-- Calculate xxHash32 hash value of a file
local checksum = hash.xxhash32("/path/to/file.txt")
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate xxHash32 hash value of binary data
local checksum = hash.xxhash32(bytes("hello"))
::: tip API
hash.xxhash64(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() function
:::Calculates hash value using the xxHash64 algorithm. Fast and suitable for quick verification.
Calculate file hash value:
-- Calculate xxHash64 hash value of a file
local checksum = hash.xxhash64("/path/to/file.txt")
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate xxHash64 hash value of binary data
local checksum = hash.xxhash64(bytes("hello"))
Incorrect usage (don't do this):
-- ❌ Wrong: Don't pass string data directly
local key = hash.xxhash64(table.concat(params, "|"))
-- ✅ Correct: Use bytes wrapper for binary data
local key = hash.xxhash64(bytes(table.concat(params, "|")))
::: tip API
hash.xxhash128(input: <string|bytes>)
:::
| Parameter | Description |
|---|---|
| input | File path (string) or binary data (bytes) |
::: warning Important Notes
bytes() function
:::Calculates hash value using the xxHash128 algorithm, providing longer hash values to reduce collisions.
Calculate file hash value:
-- Calculate xxHash128 hash value of a file
local checksum = hash.xxhash128("/path/to/file.txt")
Calculate binary data hash value:
import("core.base.bytes")
-- Calculate xxHash128 hash value of binary data
local checksum = hash.xxhash128(bytes("hello"))
::: tip API
hash.strhash32(input: <string>)
:::
| Parameter | Description |
|---|---|
| input | String |
Generates a 32-bit hash value from a string, returns format like: 91e8ecf1
This interface uses xxhash32 internally, specifically designed for fast string hashing.
::: tip API
hash.strhash64(input: <string>)
:::
| Parameter | Description |
|---|---|
| input | String |
Generates a 64-bit hash value from a string, returns format like: 91e8ecf191e8ecf1
::: tip API
hash.strhash128(input: <string>)
:::
| Parameter | Description |
|---|---|
| input | String |
Generates a 128-bit hash value from a string, returns format like: 91e8ecf1417f4edfa574e22d7d8d204a
Suitable for generating compilation cache keys:
-- Generate key for compilation cache
local cache_key = hash.strhash128(compiler .. flags .. source)
::: tip API
hash.rand32()
:::
No parameters required for this function.
Generates a 32-bit random hash value.
::: warning WARNING This interface is prone to hash collisions and is not recommended for scenarios requiring high uniqueness. :::
::: tip API
hash.rand64()
:::
No parameters required for this function.
Generates a 64-bit random hash value.
::: tip API
hash.rand128()
:::
No parameters required for this function.
Generates a 128-bit random hash value.