diff --git a/.github/workflows/ci-macos.yml b/.github/workflows/ci-macos.yml index e1235b97a70267b96cbaaf346266eee4668de627..c47c8cce328fbb985260ae5160ce057e053d40e7 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 0000000000000000000000000000000000000000..a8a37321849b19c6291cf1bde5b46d63f2cd90a6 --- /dev/null +++ b/devel/210_2.md @@ -0,0 +1,7 @@ +# [210_2] 新增返回md5摘要 + +## 2025/12/24 重构代码 + +## 2025/12/23 新增sha1和sha256 + +## 2025/12/23 新增返回md5摘要 diff --git a/goldfish/liii/hashlib.scm b/goldfish/liii/hashlib.scm new file mode 100644 index 0000000000000000000000000000000000000000..f9106908c88e85097b97eac84c9f16c3e723d4b9 --- /dev/null +++ b/goldfish/liii/hashlib.scm @@ -0,0 +1,25 @@ +; +; 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 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 bf393bf992ea5d5efd2147cc30efa793b1380c1d..22c5228694b5caf0c5cce3abdeda9fa704bd2260 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -581,6 +581,87 @@ 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); +} + +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_hashlib (s7_scheme* sc) { + glue_md5 (sc); + glue_sha1 (sc); + glue_sha256 (sc); +} + + + static s7_pointer f_isdir (s7_scheme* sc, s7_pointer args) { const char* dir_c= s7_string (s7_car (args)); @@ -1023,6 +1104,7 @@ glue_for_community_edition (s7_scheme* sc) { glue_liii_time (sc); glue_liii_datetime (sc); glue_liii_uuid (sc); + glue_liii_hashlib (sc); } static void diff --git a/tests/goldfish/liii/hashlib-test.scm b/tests/goldfish/liii/hashlib-test.scm new file mode 100644 index 0000000000000000000000000000000000000000..4a82662663c20248fd753dae52e40cb99ec319ba --- /dev/null +++ b/tests/goldfish/liii/hashlib-test.scm @@ -0,0 +1,45 @@ +; +; 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") + +;; 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