From ee2aa5b6d037d3748673481db409698199c4feaf Mon Sep 17 00:00:00 2001 From: xx Date: Fri, 14 Mar 2025 14:05:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0AES?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/codec.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/module/codec.h | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/src/module/codec.cpp b/src/module/codec.cpp index 5d237db..da4aa3f 100644 --- a/src/module/codec.cpp +++ b/src/module/codec.cpp @@ -54,6 +54,45 @@ std::string module::codec::hmac_sha256(const std::string_view& key, const std::s { return ylib::codec::hmac_sha256(ylib::buffer(key.data(), key.length()),ylib::buffer(value.data(), value.length())).to_hex(); } +std::string module::codec::aes_encode(const std::string_view& value, const std::string_view& key, const std::string& variant, const std::string& mode) +{ + ylib::codec::aes::variant v = ylib::codec::aes::variant::AES256;; + ylib::codec::aes::mode m = ylib::codec::aes::mode::CBC; + + if (variant == "aes-256") + v = ylib::codec::aes::variant::AES256; + else if (variant == "aes-192") + v = ylib::codec::aes::variant::AES192; + else if (variant == "aes-128") + v = ylib::codec::aes::variant::AES128; + + if (variant == "cbc") + m = ylib::codec::aes::mode::CBC; + else if (variant == "ebc") + m = ylib::codec::aes::mode::ECB; + + return ylib::codec::aes::en(ylib::buffer(value.data(), value.length()), ylib::buffer(key.data(), key.length()), v, m).to_hex(); + +} +std::string module::codec::aes_decode(const std::string_view& value, const std::string_view& key, const std::string& variant, const std::string& mode) +{ + ylib::codec::aes::variant v = ylib::codec::aes::variant::AES256;; + ylib::codec::aes::mode m = ylib::codec::aes::mode::CBC; + + if (variant == "aes-256") + v = ylib::codec::aes::variant::AES256; + else if (variant == "aes-192") + v = ylib::codec::aes::variant::AES192; + else if (variant == "aes-128") + v = ylib::codec::aes::variant::AES128; + + if (variant == "cbc") + m = ylib::codec::aes::mode::CBC; + else if (variant == "ebc") + m = ylib::codec::aes::mode::ECB; + + return ylib::codec::aes::de(ylib::buffer(value.data(), value.length()), ylib::buffer(key.data(), key.length()), v, m).to_hex(); +} void module::codec::regist(sol::state* lua) { lua->new_usertype("fw_codec", diff --git a/src/module/codec.h b/src/module/codec.h index 8092fe8..b1f3978 100644 --- a/src/module/codec.h +++ b/src/module/codec.h @@ -34,6 +34,10 @@ namespace module static std::string sha256(const std::string_view& value); static std::string hmac_sha256(const std::string_view& key,const std::string_view& value); + static std::string aes_encode(const std::string_view& value, const std::string_view& key, const std::string& variant,const std::string& mode); + static std::string aes_decode(const std::string_view& value,const std::string_view& key, , const std::string& variant, const std::string& mode); + + static void regist(sol::state* lua); };