summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-19 00:57:10 +0800
committerruki <[email protected]>2022-05-19 00:57:10 +0800
commit961f414fb459c02ba82ceff540296b52e3a965bd (patch)
tree61635f96a456daada00f5172822a8df47359eb27
parente0e5d36e264fc580802c93e26103d604e61f6a00 (diff)
add hash.sha1
-rw-r--r--core/src/xmake/engine.c4
-rw-r--r--core/src/xmake/hash/sha.c (renamed from core/src/xmake/hash/sha256.c)33
-rw-r--r--core/src/xmake/makefile2
-rw-r--r--xmake/core/base/hash.lua21
-rw-r--r--xmake/core/sandbox/modules/hash.lua9
-rw-r--r--xmake/modules/private/cache/build_cache.lua2
6 files changed, 48 insertions, 23 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 43e073210..a5ebeb9d7 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -182,7 +182,7 @@ tb_int_t xm_path_is_absolute(lua_State* lua);
// the hash functions
tb_int_t xm_hash_uuid4(lua_State* lua);
-tb_int_t xm_hash_sha256(lua_State* lua);
+tb_int_t xm_hash_sha(lua_State* lua);
tb_int_t xm_hash_md5(lua_State* lua);
// the base64 functions
@@ -406,7 +406,7 @@ static luaL_Reg const g_path_functions[] =
static luaL_Reg const g_hash_functions[] =
{
{ "uuid4", xm_hash_uuid4 }
-, { "sha256", xm_hash_sha256 }
+, { "sha", xm_hash_sha }
, { "md5", xm_hash_md5 }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/hash/sha256.c b/core/src/xmake/hash/sha.c
index 65f1139a6..a89ee5df7 100644
--- a/core/src/xmake/hash/sha256.c
+++ b/core/src/xmake/hash/sha.c
@@ -15,14 +15,14 @@
* Copyright (C) 2015-present, TBOOX Open Source Group.
*
* @author ruki
- * @file sha256.c
+ * @file sha.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
-#define TB_TRACE_MODULE_NAME "sha256"
+#define TB_TRACE_MODULE_NAME "sha"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -33,16 +33,19 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-tb_int_t xm_hash_sha256(lua_State* lua)
+tb_int_t xm_hash_sha(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
+ // get mode
+ tb_size_t mode = (tb_size_t)lua_tonumber(lua, 1);
+
// is bytes? get data and size
- if (lua_isnumber(lua, 1) && lua_isnumber(lua, 2))
+ if (lua_isnumber(lua, 2) && lua_isnumber(lua, 3))
{
- tb_byte_t const* data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 1);
- tb_size_t size = (tb_size_t)lua_tonumber(lua, 2);
+ tb_byte_t const* data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 2);
+ tb_size_t size = (tb_size_t)lua_tonumber(lua, 3);
if (!data || !size)
{
lua_pushnil(lua);
@@ -50,14 +53,14 @@ tb_int_t xm_hash_sha256(lua_State* lua)
return 2;
}
- // compute sha256
+ // compute sha
tb_sha_t sha;
tb_byte_t buffer[32];
- tb_sha_init(&sha, TB_SHA_MODE_SHA2_256);
+ tb_sha_init(&sha, mode);
tb_sha_spak(&sha, data, size);
tb_sha_exit(&sha, buffer, sizeof(buffer));
- // make sha256 string
+ // make sha string
tb_size_t i = 0;
tb_size_t n = sha.digest_len << 2;
tb_char_t s[256] = {0};
@@ -69,7 +72,7 @@ tb_int_t xm_hash_sha256(lua_State* lua)
}
// get the filename
- tb_char_t const* filename = luaL_checkstring(lua, 1);
+ tb_char_t const* filename = luaL_checkstring(lua, 2);
tb_check_return_val(filename, 0);
// load data from file
@@ -80,11 +83,11 @@ tb_int_t xm_hash_sha256(lua_State* lua)
// open stream
if (tb_stream_open(stream))
{
- // init sha256
+ // init sha
tb_sha_t sha;
- tb_sha_init(&sha, TB_SHA_MODE_SHA2_256);
+ tb_sha_init(&sha, mode);
- // read data and update sha256
+ // read data and update sha
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
while (!tb_stream_beof(stream))
{
@@ -107,11 +110,11 @@ tb_int_t xm_hash_sha256(lua_State* lua)
else break;
}
- // exit sha256
+ // exit sha
tb_byte_t buffer[32];
tb_sha_exit(&sha, buffer, sizeof(buffer));
- // make sha256 string
+ // make sha string
tb_size_t i = 0;
tb_size_t n = sha.digest_len << 2;
tb_char_t s[256] = {0};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index b9a40c676..c34b1ebdc 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -99,7 +99,7 @@ xmake_C_FILES += \
path/directory \
path/is_absolute \
hash/uuid4 \
- hash/sha256 \
+ hash/sha \
hash/md5 \
base64/encode \
base64/decode \
diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua
index 88bdbe673..f76c3812a 100644
--- a/xmake/core/base/hash.lua
+++ b/xmake/core/base/hash.lua
@@ -27,8 +27,8 @@ local utils = require("base/utils")
local bytes = require("base/bytes")
-- save metatable and builtin functions
-hash._md5 = hash._md5 or hash.md5
-hash._sha256 = hash._sha256 or hash.sha256
+hash._md5 = hash._md5 or hash.md5
+hash._sha = hash._sha or hash.sha
-- make md5 from the given file or data
function hash.md5(file_or_data)
@@ -43,15 +43,28 @@ function hash.md5(file_or_data)
return hashstr, errors
end
+-- make sha1 from the given file or data
+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
+
-- make sha256 from the given file or data
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._sha256(dataaddr, datasize)
+ hashstr, errors = hash._sha(256, dataaddr, datasize)
else
- hashstr, errors = hash._sha256(file_or_data)
+ hashstr, errors = hash._sha(256, file_or_data)
end
return hashstr, errors
end
diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua
index c4bcba57a..95bc4317f 100644
--- a/xmake/core/sandbox/modules/hash.lua
+++ b/xmake/core/sandbox/modules/hash.lua
@@ -39,6 +39,15 @@ function sandbox_hash.uuid4(name)
return uuid
end
+-- make sha1 from the given file or data
+function sandbox_hash.sha1(file_or_data)
+ local sha1, errors = hash.sha1(file_or_data)
+ if not sha1 then
+ raise("cannot make sha1 for %s, %s", file_or_data, errors or "unknown errors")
+ end
+ return sha1
+end
+
-- make sha256 from the given file or data
function sandbox_hash.sha256(file_or_data)
local sha256, errors = hash.sha256(file_or_data)
diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua
index 0144317aa..440dfb9a2 100644
--- a/xmake/modules/private/cache/build_cache.lua
+++ b/xmake/modules/private/cache/build_cache.lua
@@ -49,7 +49,7 @@ function cachekey(program, cppfile, cppflags, envs)
table.insert(items, cppflag)
end
table.sort(items)
- table.insert(items, hash.sha256(cppfile))
+ table.insert(items, hash.sha1(cppfile))
if envs then
local basename = path.basename(program)
if basename == "cl" then