add security section

This commit is contained in:
chatlanin 2025-01-13 21:36:40 +03:00
parent 096ef69938
commit 159dfed234
4 changed files with 69 additions and 0 deletions

14
src/hack/security/is_link.hpp Executable file
View File

@ -0,0 +1,14 @@
#pragma once
#include <uuid/uuid.h>
#include <regex>
#include <string>
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);
}
}

15
src/hack/security/is_string.hpp Executable file
View File

@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <type_traits>
namespace hack::security
{
template<typename T>
struct is_string : public std::disjunction<std::is_same<char*,
typename std::decay_t<T>>,
std::is_same<const char*,
typename std::decay_t<T>>,
std::is_same<std::string,
typename std::decay_t<T>>> {};
}

27
src/hack/security/uuid.hpp Executable file
View File

@ -0,0 +1,27 @@
#pragma once
#include <uuid/uuid.h>
#include <regex>
#include <string>
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);
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include <regex>
#include <string>
namespace hack::security
{
inline bool validate_email(std::string& email)
{
const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
return std::regex_match(email, pattern);
}
}