You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -5,13 +5,13 @@ The hash module provides hash value calculation and UUID generation functions. I
5
5
6
6
## hash.md5
7
7
8
-
- Calculate the MD5 hash value of a string or file
8
+
- Calculate the MD5 hash value of a file or binary data
9
9
10
10
#### Function Prototype
11
11
12
12
::: tip API
13
13
```lua
14
-
hash.md5(input: <string>)
14
+
hash.md5(input: <string|bytes>)
15
15
```
16
16
:::
17
17
@@ -20,35 +20,59 @@ hash.md5(input: <string>)
20
20
21
21
| Parameter | Description |
22
22
|-----------|-------------|
23
-
| input | String or file path |
23
+
| input | File path (string) or binary data (bytes) |
24
+
25
+
::: warning Important Notes
26
+
-**String parameter is only for file paths**: If a string is passed, the function will calculate the hash value of that file
27
+
-**Binary data must use bytes parameter**: To calculate the hash value of binary data, you must wrap the data with the `bytes()` function
28
+
-**String data hashing**: To calculate the hash value of string data, please use `hash.strhash32`, `hash.strhash64`, or `hash.strhash128` interfaces
29
+
-**Common mistake**: If the file doesn't exist, the function will incorrectly treat the file path as string data to calculate the hash value, and it won't report an error, resulting in incorrect results
30
+
:::
24
31
25
32
#### Usage
26
33
34
+
Calculates the MD5 hash value of the specified file or binary data and returns a hexadecimal format hash string.
35
+
36
+
**Calculate file hash value:**
37
+
27
38
```lua
28
-
localhashval=hash.md5("hello")
29
-
localhashval=hash.md5("/path/to/file")
39
+
-- Calculate MD5 hash value of a file
40
+
localchecksum=hash.md5("/path/to/file.txt")
41
+
print("MD5: " ..checksum)
30
42
```
31
43
32
-
Calculates the MD5 hash value of the specified string or file and returns a hexadecimal format hash string. Supports both string and file path as input.
33
-
34
-
Commonly used for calculating file content checksums:
44
+
**Calculate binary data hash value:**
35
45
36
46
```lua
37
-
-- Read file content and calculate MD5
38
-
localcontent=io.readfile("file.txt")
39
-
localchecksum=hash.md5(content)
47
+
import("core.base.bytes")
48
+
49
+
-- Calculate MD5 hash value of binary data
50
+
localdata=bytes("hello")
51
+
localchecksum=hash.md5(data)
40
52
print("MD5: " ..checksum)
41
53
```
42
54
55
+
**Incorrect usage (don't do this):**
56
+
57
+
```lua
58
+
-- ❌ Wrong: If "hello" is not a file path, it will incorrectly calculate the hash of the file path string
59
+
localchecksum=hash.md5("hello")
60
+
61
+
-- ✅ Correct: Use strhash interface to calculate string hash value
62
+
localchecksum=hash.strhash32("hello")
63
+
-- Or use bytes wrapper
64
+
localchecksum=hash.md5(bytes("hello"))
65
+
```
66
+
43
67
## hash.sha1
44
68
45
-
- Calculate the SHA1 hash value of a string or file
69
+
- Calculate the SHA1 hash value of a file or binary data
46
70
47
71
#### Function Prototype
48
72
49
73
::: tip API
50
74
```lua
51
-
hash.sha1(input: <string>)
75
+
hash.sha1(input: <string|bytes>)
52
76
```
53
77
:::
54
78
@@ -57,26 +81,43 @@ hash.sha1(input: <string>)
57
81
58
82
| Parameter | Description |
59
83
|-----------|-------------|
60
-
| input | String or file path |
84
+
| input | File path (string) or binary data (bytes) |
85
+
86
+
::: warning Important Notes
87
+
-**String parameter is only for file paths**: If a string is passed, the function will calculate the hash value of that file
88
+
-**Binary data must use bytes parameter**: To calculate the hash value of binary data, you must wrap the data with the `bytes()` function
89
+
-**String data hashing**: To calculate the hash value of string data, please use `hash.strhash32`, `hash.strhash64`, or `hash.strhash128` interfaces
90
+
:::
61
91
62
92
#### Usage
63
93
94
+
Calculates the SHA1 hash value of the specified file or binary data and returns a hexadecimal format hash string.
95
+
96
+
**Calculate file hash value:**
97
+
64
98
```lua
65
-
localhashval=hash.sha1("hello")
66
-
localhashval=hash.sha1("/path/to/file")
99
+
-- Calculate SHA1 hash value of a file
100
+
localchecksum=hash.sha1("/path/to/file.txt")
67
101
```
68
102
69
-
Calculates the SHA1 hash value of the specified string or file and returns a hexadecimal format hash string.
103
+
**Calculate binary data hash value:**
104
+
105
+
```lua
106
+
import("core.base.bytes")
107
+
108
+
-- Calculate SHA1 hash value of binary data
109
+
localchecksum=hash.sha1(bytes("hello"))
110
+
```
70
111
71
112
## hash.sha256
72
113
73
-
- Calculate the SHA256 hash value of a string or file
114
+
- Calculate the SHA256 hash value of a file or binary data
74
115
75
116
#### Function Prototype
76
117
77
118
::: tip API
78
119
```lua
79
-
hash.sha256(input: <string>)
120
+
hash.sha256(input: <string|bytes>)
80
121
```
81
122
:::
82
123
@@ -85,19 +126,22 @@ hash.sha256(input: <string>)
85
126
86
127
| Parameter | Description |
87
128
|-----------|-------------|
88
-
| input |String or file path|
129
+
| input |File path (string) or binary data (bytes)|
89
130
90
-
#### Usage
131
+
::: warning Important Notes
132
+
-**String parameter is only for file paths**: If a string is passed, the function will calculate the hash value of that file
133
+
-**Binary data must use bytes parameter**: To calculate the hash value of binary data, you must wrap the data with the `bytes()` function
134
+
-**String data hashing**: To calculate the hash value of string data, please use `hash.strhash32`, `hash.strhash64`, or `hash.strhash128` interfaces
135
+
:::
91
136
92
-
```lua
93
-
localhashval=hash.sha256("hello")
94
-
localhashval=hash.sha256("/path/to/file")
95
-
```
137
+
#### Usage
96
138
97
-
Calculates the SHA256 hash value of the specified string or file and returns a hexadecimal format hash string.
139
+
Calculates the SHA256 hash value of the specified file or binary data and returns a hexadecimal format hash string.
98
140
99
141
SHA256 is more secure than MD5 and is commonly used for package integrity verification:
100
142
143
+
**Calculate file hash value:**
144
+
101
145
```lua
102
146
-- Verify downloaded package file
103
147
localpackagefile="package.tar.gz"
@@ -107,6 +151,15 @@ if checksum ~= expected_hash then
107
151
end
108
152
```
109
153
154
+
**Calculate binary data hash value:**
155
+
156
+
```lua
157
+
import("core.base.bytes")
158
+
159
+
-- Calculate SHA256 hash value of binary data
160
+
localchecksum=hash.sha256(bytes("hello"))
161
+
```
162
+
110
163
## hash.uuid
111
164
112
165
- Generate a UUID based on a name
@@ -145,13 +198,13 @@ local config_id = hash.uuid("debug-x64-windows")
145
198
146
199
## hash.xxhash32
147
200
148
-
- Calculate the 32-bit xxHash hash value of a string or file
201
+
- Calculate the 32-bit xxHash hash value of a file or binary data
| input | File path (string) or binary data (bytes) |
217
+
218
+
::: warning Important Notes
219
+
-**String parameter is only for file paths**: If a string is passed, the function will calculate the hash value of that file
220
+
-**Binary data must use bytes parameter**: To calculate the hash value of binary data, you must wrap the data with the `bytes()` function
221
+
-**String data hashing**: To calculate the hash value of string data, please use `hash.strhash32` interface
222
+
:::
164
223
165
224
#### Usage
166
225
167
226
Calculates hash value using the xxHash32 algorithm. xxHash is an extremely fast non-cryptographic hash algorithm suitable for hash tables, checksums, and other scenarios.
168
227
228
+
**Calculate file hash value:**
229
+
230
+
```lua
231
+
-- Calculate xxHash32 hash value of a file
232
+
localchecksum=hash.xxhash32("/path/to/file.txt")
233
+
```
234
+
235
+
**Calculate binary data hash value:**
236
+
237
+
```lua
238
+
import("core.base.bytes")
239
+
240
+
-- Calculate xxHash32 hash value of binary data
241
+
localchecksum=hash.xxhash32(bytes("hello"))
242
+
```
243
+
169
244
## hash.xxhash64
170
245
171
-
- Calculate the 64-bit xxHash hash value of a string or file
246
+
- Calculate the 64-bit xxHash hash value of a file or binary data
0 commit comments