Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 2 additions & 0 deletions lib/include/pl/lib/std/libstd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -30,6 +31,7 @@ namespace pl::lib::libstd {
core::registerFunctions(runtime);
hash::registerFunctions(runtime);
random::registerFunctions(runtime);
crypto::registerFunctions(runtime);
}

}
36 changes: 36 additions & 0 deletions lib/source/pl/lib/std/crypto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <numeric>

#include <pl.hpp>


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<Token::Literal> {
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());
});

}
}

}