From a6cb29b57e66b18a55b8aa667b0f805b3f785ffa Mon Sep 17 00:00:00 2001 From: wumo <121778696+17802723@users.noreply.github.com> Date: Tue, 23 Dec 2025 20:37:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[210=5F2]=20=E6=96=B0=E5=A2=9E=E8=BF=94?= =?UTF-8?q?=E5=9B=9Emd5=E6=91=98=E8=A6=81=20(#345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci-macos.yml | 2 +- devel/210_2.md | 3 +++ goldfish/liii/hashlib.scm | 23 ++++++++++++++++++++ src/goldfish.hpp | 32 ++++++++++++++++++++++++++++ tests/goldfish/liii/hashlib-test.scm | 27 +++++++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 devel/210_2.md create mode 100644 goldfish/liii/hashlib.scm create mode 100644 tests/goldfish/liii/hashlib-test.scm diff --git a/.github/workflows/ci-macos.yml b/.github/workflows/ci-macos.yml index e1235b97..c47c8cce 100644 --- a/.github/workflows/ci-macos.yml +++ b/.github/workflows/ci-macos.yml @@ -24,7 +24,7 @@ on: jobs: macosbuild: - runs-on: macos-13 + runs-on: macos-14 steps: - uses: actions/checkout@v2 with: diff --git a/devel/210_2.md b/devel/210_2.md new file mode 100644 index 00000000..bf66d60e --- /dev/null +++ b/devel/210_2.md @@ -0,0 +1,3 @@ +# [210_2] 新增返回md5摘要 + +## 2025/12/23 diff --git a/goldfish/liii/hashlib.scm b/goldfish/liii/hashlib.scm new file mode 100644 index 00000000..b6995ebf --- /dev/null +++ b/goldfish/liii/hashlib.scm @@ -0,0 +1,23 @@ +; +; Copyright (C) 2025 The Goldfish Scheme Authors +; +; 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. +; +(define-library (liii hashlib) + (export md5) + (begin + + (define (md5 str) (g_md5 str)) + + ) ; end of begin + ) ; end of define-library diff --git a/src/goldfish.hpp b/src/goldfish.hpp index bf393bf9..37e79945 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -581,6 +581,37 @@ glue_liii_uuid (s7_scheme* sc) { glue_uuid4 (sc); } +static s7_pointer f_md5(s7_scheme* sc, s7_pointer args) { + const char* searchString = s7_string(s7_car(args)); + tb_size_t len = tb_strlen(searchString); + tb_byte_t ob[16]; + tb_char_t hex_output[33] = {0}; + tb_md5_t md5; + tb_md5_init(&md5, 0); + if (len > 0) { + tb_md5_spak(&md5, (tb_byte_t const*)searchString, len); + } + tb_md5_exit(&md5, ob, 16); + for (tb_size_t i = 0; i < 16; ++i) { + tb_snprintf(hex_output + (i << 1), 3, "%02x", ob[i]); + } + return s7_make_string(sc, hex_output); +} + +inline void +glue_md5(s7_scheme* sc) { + const char* name = "g_md5"; + const char* desc = "(g_md5 str) => string"; + glue_define(sc, name, desc, f_md5, 1, 0); +} + +inline void +glue_liii_md5 (s7_scheme* sc) { + glue_md5 (sc); +} + + + static s7_pointer f_isdir (s7_scheme* sc, s7_pointer args) { const char* dir_c= s7_string (s7_car (args)); @@ -1023,6 +1054,7 @@ glue_for_community_edition (s7_scheme* sc) { glue_liii_time (sc); glue_liii_datetime (sc); glue_liii_uuid (sc); + glue_liii_md5(sc); } static void diff --git a/tests/goldfish/liii/hashlib-test.scm b/tests/goldfish/liii/hashlib-test.scm new file mode 100644 index 00000000..cc2aa221 --- /dev/null +++ b/tests/goldfish/liii/hashlib-test.scm @@ -0,0 +1,27 @@ +; +; Copyright (C) 2025 The Goldfish Scheme Authors +; +; 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. +; + +(import (liii check) (liii hashlib)) + +(check (md5 "") => "d41d8cd98f00b204e9800998ecf8427e") +(check (md5 "hello") => "5d41402abc4b2a76b9719d911017c592") +(check (md5 "The quick brown fox jumps over the lazy dog") => "9e107d9d372bb6826bd81d3542a419d6") +(check (md5 "a") => "0cc175b9c0f1b6a831c399e269772661") +(check (md5 "123456") => "e10adc3949ba59abbe56e057f20f883e") +(check (md5 "!@#$%^&*()") => "05b28d17a7b6e7024b6e5d8cc43a8bf7") +(check (md5 "Hello") => "8b1a9953c4611296a827abf8c47804d7") + +(check-report) \ No newline at end of file -- Gitee From 906ee96ec0194fe5c47a151b8f7daac290eee195 Mon Sep 17 00:00:00 2001 From: wumo <121778696+17802723@users.noreply.github.com> Date: Tue, 23 Dec 2025 21:30:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[210=5F2]=20=E6=96=B0=E5=A2=9Esha1=E5=92=8C?= =?UTF-8?q?sha256=20(#346)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devel/210_2.md | 4 +- goldfish/liii/hashlib.scm | 4 +- src/goldfish.hpp | 60 ++++++++++++++++++++++++++++ tests/goldfish/liii/hashlib-test.scm | 18 +++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/devel/210_2.md b/devel/210_2.md index bf66d60e..bb16dadc 100644 --- a/devel/210_2.md +++ b/devel/210_2.md @@ -1,3 +1,5 @@ # [210_2] 新增返回md5摘要 -## 2025/12/23 +## 2025/12/23 新增sha1和sha256 + +## 2025/12/23 新增返回md5摘要 diff --git a/goldfish/liii/hashlib.scm b/goldfish/liii/hashlib.scm index b6995ebf..f9106908 100644 --- a/goldfish/liii/hashlib.scm +++ b/goldfish/liii/hashlib.scm @@ -14,10 +14,12 @@ ; under the License. ; (define-library (liii hashlib) - (export md5) + (export md5 sha1 sha256) (begin (define (md5 str) (g_md5 str)) + (define (sha1 str) (g_sha1 str)) + (define (sha256 str) (g_sha256 str)) ) ; end of begin ) ; end of define-library diff --git a/src/goldfish.hpp b/src/goldfish.hpp index 37e79945..cf765b1e 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -610,6 +610,64 @@ glue_liii_md5 (s7_scheme* sc) { glue_md5 (sc); } +static s7_pointer f_sha1(s7_scheme* sc, s7_pointer args) { + const char* searchString = s7_string(s7_car(args)); + tb_size_t len = tb_strlen(searchString); + tb_byte_t ob[20]; // SHA1 produces 20 bytes + tb_char_t hex_output[41] = {0}; // 20 bytes * 2 hex digits per byte + null terminator + tb_sha_t sha; + tb_sha_init(&sha, 160); // TB_SHA_MODE_SHA1_160 = 160 + if (len > 0) { + tb_sha_spak(&sha, (tb_byte_t const*)searchString, len); + } + tb_sha_exit(&sha, ob, 20); + for (tb_size_t i = 0; i < 20; ++i) { + tb_snprintf(hex_output + (i << 1), 3, "%02x", ob[i]); + } + return s7_make_string(sc, hex_output); +} + +inline void +glue_sha1(s7_scheme* sc) { + const char* name = "g_sha1"; + const char* desc = "(g_sha1 str) => string"; + glue_define(sc, name, desc, f_sha1, 1, 0); +} + +static s7_pointer f_sha256(s7_scheme* sc, s7_pointer args) { + const char* searchString = s7_string(s7_car(args)); + tb_size_t len = tb_strlen(searchString); + tb_byte_t ob[32]; // SHA256 produces 32 bytes + tb_char_t hex_output[65] = {0}; // 32 bytes * 2 hex digits per byte + null terminator + tb_sha_t sha; + tb_sha_init(&sha, 256); // TB_SHA_MODE_SHA2_256 = 256 + if (len > 0) { + tb_sha_spak(&sha, (tb_byte_t const*)searchString, len); + } + tb_sha_exit(&sha, ob, 32); + for (tb_size_t i = 0; i < 32; ++i) { + tb_snprintf(hex_output + (i << 1), 3, "%02x", ob[i]); + } + return s7_make_string(sc, hex_output); +} + +inline void +glue_sha256(s7_scheme* sc) { + const char* name = "g_sha256"; + const char* desc = "(g_sha256 str) => string"; + glue_define(sc, name, desc, f_sha256, 1, 0); +} + +inline void +glue_liii_sha1 (s7_scheme* sc) { + glue_sha1 (sc); +} + +inline void +glue_liii_sha256 (s7_scheme* sc) { + glue_sha256 (sc); +} + static s7_pointer @@ -1055,6 +1113,8 @@ glue_for_community_edition (s7_scheme* sc) { glue_liii_datetime (sc); glue_liii_uuid (sc); glue_liii_md5(sc); + glue_liii_sha1(sc); + glue_liii_sha256(sc); } static void diff --git a/tests/goldfish/liii/hashlib-test.scm b/tests/goldfish/liii/hashlib-test.scm index cc2aa221..4a826626 100644 --- a/tests/goldfish/liii/hashlib-test.scm +++ b/tests/goldfish/liii/hashlib-test.scm @@ -24,4 +24,22 @@ (check (md5 "!@#$%^&*()") => "05b28d17a7b6e7024b6e5d8cc43a8bf7") (check (md5 "Hello") => "8b1a9953c4611296a827abf8c47804d7") +;; SHA1 tests +(check (sha1 "") => "da39a3ee5e6b4b0d3255bfef95601890afd80709") +(check (sha1 "hello") => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d") +(check (sha1 "The quick brown fox jumps over the lazy dog") => "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") +(check (sha1 "a") => "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8") +(check (sha1 "123456") => "7c4a8d09ca3762af61e59520943dc26494f8941b") +(check (sha1 "!@#$%^&*()") => "bf24d65c9bb05b9b814a966940bcfa50767c8a8d") +(check (sha1 "Hello") => "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0") + +;; SHA256 tests +(check (sha256 "") => "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") +(check (sha256 "hello") => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") +(check (sha256 "The quick brown fox jumps over the lazy dog") => "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592") +(check (sha256 "a") => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb") +(check (sha256 "123456") => "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92") +(check (sha256 "!@#$%^&*()") => "95ce789c5c9d18490972709838ca3a9719094bca3ac16332cfec0652b0236141") +(check (sha256 "Hello") => "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969") + (check-report) \ No newline at end of file -- Gitee From dc567a5c86d70ee64bd02ae73d6d31cc29ff5c3b Mon Sep 17 00:00:00 2001 From: Da Shen Date: Wed, 24 Dec 2025 11:40:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[210=5F2]=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devel/210_2.md | 2 ++ src/goldfish.hpp | 16 +++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/devel/210_2.md b/devel/210_2.md index bb16dadc..a8a37321 100644 --- a/devel/210_2.md +++ b/devel/210_2.md @@ -1,5 +1,7 @@ # [210_2] 新增返回md5摘要 +## 2025/12/24 重构代码 + ## 2025/12/23 新增sha1和sha256 ## 2025/12/23 新增返回md5摘要 diff --git a/src/goldfish.hpp b/src/goldfish.hpp index cf765b1e..22c52286 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -605,11 +605,6 @@ glue_md5(s7_scheme* sc) { glue_define(sc, name, desc, f_md5, 1, 0); } -inline void -glue_liii_md5 (s7_scheme* sc) { - glue_md5 (sc); -} - static s7_pointer f_sha1(s7_scheme* sc, s7_pointer args) { const char* searchString = s7_string(s7_car(args)); tb_size_t len = tb_strlen(searchString); @@ -659,12 +654,9 @@ glue_sha256(s7_scheme* sc) { } inline void -glue_liii_sha1 (s7_scheme* sc) { +glue_liii_hashlib (s7_scheme* sc) { + glue_md5 (sc); glue_sha1 (sc); -} - -inline void -glue_liii_sha256 (s7_scheme* sc) { glue_sha256 (sc); } @@ -1112,9 +1104,7 @@ glue_for_community_edition (s7_scheme* sc) { glue_liii_time (sc); glue_liii_datetime (sc); glue_liii_uuid (sc); - glue_liii_md5(sc); - glue_liii_sha1(sc); - glue_liii_sha256(sc); + glue_liii_hashlib (sc); } static void -- Gitee