From 159dfed23432c1fdb12512f16f124c4292b40e66 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Mon, 13 Jan 2025 21:36:40 +0300 Subject: [PATCH] add security section --- src/hack/security/is_link.hpp | 14 ++++++++++++++ src/hack/security/is_string.hpp | 15 +++++++++++++++ src/hack/security/uuid.hpp | 27 +++++++++++++++++++++++++++ src/hack/security/validate_email.hpp | 13 +++++++++++++ 4 files changed, 69 insertions(+) create mode 100755 src/hack/security/is_link.hpp create mode 100755 src/hack/security/is_string.hpp create mode 100755 src/hack/security/uuid.hpp create mode 100755 src/hack/security/validate_email.hpp diff --git a/src/hack/security/is_link.hpp b/src/hack/security/is_link.hpp new file mode 100755 index 0000000..2dc045f --- /dev/null +++ b/src/hack/security/is_link.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include +#include + +namespace hack::security +{ + inline bool is_link(const std::string& s) + { + static const std::regex e("^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$", std::regex_constants::icase); + return std::regex_match (s, e); + } +} diff --git a/src/hack/security/is_string.hpp b/src/hack/security/is_string.hpp new file mode 100755 index 0000000..c4c7e50 --- /dev/null +++ b/src/hack/security/is_string.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +namespace hack::security +{ + template + struct is_string : public std::disjunction>, + std::is_same>, + std::is_same>> {}; +} diff --git a/src/hack/security/uuid.hpp b/src/hack/security/uuid.hpp new file mode 100755 index 0000000..50c90fd --- /dev/null +++ b/src/hack/security/uuid.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace hack::security +{ + inline std::string generate_uuid() + { + std::string uuid; + uuid_t uuid_obj; + uuid_generate_time_safe(uuid_obj); + char uuid_ch[UUID_STR_LEN]; + uuid_unparse_lower(uuid_obj, uuid_ch); + std::stringstream ss; + ss << uuid_ch; + ss >> uuid; + return uuid; + } + + inline bool validate_uuid(const std::string& s) + { + static const std::regex e("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$", std::regex_constants::icase); + return std::regex_match(s, e); + } +} diff --git a/src/hack/security/validate_email.hpp b/src/hack/security/validate_email.hpp new file mode 100755 index 0000000..44a8e0d --- /dev/null +++ b/src/hack/security/validate_email.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +namespace hack::security +{ + inline bool validate_email(std::string& email) + { + const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"); + return std::regex_match(email, pattern); + } +}