diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 772f4168..bbb54390 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -74,6 +74,7 @@ add_library(libpl ${LIBRARY_TYPE} source/pl/lib/std/core.cpp source/pl/lib/std/hash.cpp source/pl/lib/std/random.cpp + source/pl/lib/std/crypto.cpp source/pl/core/resolvers.cpp ) diff --git a/lib/include/pl/lib/std/libstd.hpp b/lib/include/pl/lib/std/libstd.hpp index 648f637e..ff8c25cd 100644 --- a/lib/include/pl/lib/std/libstd.hpp +++ b/lib/include/pl/lib/std/libstd.hpp @@ -17,6 +17,7 @@ namespace pl::lib::libstd { namespace core { void registerFunctions(pl::PatternLanguage &runtime); } namespace hash { void registerFunctions(pl::PatternLanguage &runtime); } namespace random { void registerFunctions(pl::PatternLanguage &runtime); } + namespace crypto { void registerFunctions(pl::PatternLanguage &runtime); } inline void registerFunctions(pl::PatternLanguage &runtime) { registerPragmas(runtime); @@ -30,6 +31,7 @@ namespace pl::lib::libstd { core::registerFunctions(runtime); hash::registerFunctions(runtime); random::registerFunctions(runtime); + crypto::registerFunctions(runtime); } } \ No newline at end of file diff --git a/lib/source/pl/lib/std/crypto.cpp b/lib/source/pl/lib/std/crypto.cpp new file mode 100644 index 00000000..fb58a9e2 --- /dev/null +++ b/lib/source/pl/lib/std/crypto.cpp @@ -0,0 +1,36 @@ +#include + +#include + + +namespace pl::lib::libstd::crypto { + + void registerFunctions(pl::PatternLanguage &runtime) { + using FunctionParameterCount = pl::api::FunctionParameterCount; + using namespace pl::core; + + api::Namespace nsStdMath = { "builtin", "std", "crypto" }; + { + /* xor(buffer, key) */ + runtime.addFunction(nsStdMath, "xor", FunctionParameterCount::exactly(2), [](Evaluator *, auto params) -> std::optional { + if (!params[0].isPattern() && !params[0].isString()) + pl::core::err::E0012.throwError("Only patterns and strings are supported for crypto::crc buffer."); + + if (!params[1].isPattern() && !params[1].isString()) + pl::core::err::E0012.throwError("Only patterns and strings are supported for crypto::crc key."); + + auto buffer = params[0].toBytes(); + auto key = params[1].toBytes(); + + auto result = buffer; + + for (std::size_t i = 0; i < result.size(); ++i) + result[i] ^= key[i % key.size()]; + + return std::string(result.begin(), result.end()); + }); + + } + } + +} \ No newline at end of file