summaryrefslogtreecommitdiff
path: root/xmake/core/base/hash.lua
blob: 4b37946e8979d620e9d6fb0ee540a53c5803ce3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        hash.lua
--

-- define module: hash
local hash  = hash or {}

-- load modules
local io    = require("base/io")
local utils = require("base/utils")
local bytes = require("base/bytes")
local libc  = require("base/libc")

-- save metatable and builtin functions
hash._md5 = hash._md5 or hash.md5
hash._sha = hash._sha or hash.sha
hash._xxhash = hash._xxhash or hash.xxhash
hash._rand32 = hash._rand32 or hash.rand32
hash._rand64 = hash._rand64 or hash.rand64
hash._rand128 = hash._rand128 or hash.rand128

-- generate md5 hash from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.md5(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._md5(dataaddr, datasize)
    else
        hashstr, errors = hash._md5(file_or_data)
    end
    return hashstr, errors
end

-- generate sha1 hash from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.sha1(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._sha(160, dataaddr, datasize)
    else
        hashstr, errors = hash._sha(160, file_or_data)
    end
    return hashstr, errors
end

-- generate sha256 hash from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.sha256(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._sha(256, dataaddr, datasize)
    else
        hashstr, errors = hash._sha(256, file_or_data)
    end
    return hashstr, errors
end

-- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A"
--
-- @param str   the seed string (optional, random if nil)
-- @return      the uuid string
--
function hash.uuid(str)
    return hash.uuid4(str)
end

-- generate xxhash32 from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.xxhash32(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._xxhash(32, dataaddr, datasize)
    else
        hashstr, errors = hash._xxhash(32, file_or_data)
    end
    return hashstr, errors
end

-- generate xxhash64 from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.xxhash64(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._xxhash(64, dataaddr, datasize)
    else
        hashstr, errors = hash._xxhash(64, file_or_data)
    end
    return hashstr, errors
end

-- generate xxhash128 from the given file or data
--
-- @param file_or_data  the file path, data string, or bytes object
-- @return              the hash hex string, or nil and error info
--
function hash.xxhash128(file_or_data)
    local hashstr, errors
    if bytes.instance_of(file_or_data) then
        local datasize = file_or_data:size()
        local dataaddr = file_or_data:caddr()
        hashstr, errors = hash._xxhash(128, dataaddr, datasize)
    else
        hashstr, errors = hash._xxhash(128, file_or_data)
    end
    return hashstr, errors
end

-- generate hash32 from string, e.g. "91e8ecf1"
--
-- @param str   the input string
-- @return      the 32-bit hash hex string
--
function hash.strhash32(str)
    if hash._rand32 then
        local data = libc.ptraddr(libc.dataptr(str))
        local size = #str
        return hash._xxhash(32, data, size)
    else
        -- we need to be compatible with the old binary core (<= v3.0.3)
        return hash.uuid4(str):split("-", {plain = true})[1]:lower()
    end
end

-- generate hash64 from string, e.g. "91e8ecf191e8ecf1"
--
-- @param str   the input string
-- @return      the 64-bit hash hex string
--
function hash.strhash64(str)
    local data = libc.ptraddr(libc.dataptr(str))
    local size = #str
    return hash._xxhash(64, data, size)
end

-- generate hash128 from string, e.g. "91e8ecf1417f4edfa574e22d7d8d204a"
--
-- @param str   the input string
-- @return      the 128-bit hash hex string
--
function hash.strhash128(str)
    local data = libc.ptraddr(libc.dataptr(str))
    local size = #str
    return hash._xxhash(128, data, size)
end

-- generate random 32-bit hash
--
-- @return      the random hash hex string
-- @note        it is easy to trigger hash conflicts
--
function hash.rand32()
    if hash._rand32 then
        return hash._rand32()
    else
        return hash.strhash32(tostring(math.random()))
    end
end

-- generate random64 hash
function hash.rand64()
    if hash._rand64 then
        return hash._rand64()
    else
        return hash.strhash64(tostring(math.random()))
    end
end

-- generate random128 hash
function hash.rand128()
    if hash._rand128 then
        return hash._rand128()
    else
        return hash.strhash128(tostring(math.random()))
    end
end

-- return module: hash
return hash