From cb354e666dc2800f621f99ae69fa9ff7ee638f73 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 11 Jun 2022 03:37:09 +0200 Subject: [PATCH] move types to types.hpp, match hash to email --- src/hash.cpp | 23 +++++++++++++++-------- src/hash.hpp | 17 ++++++----------- src/main.cpp | 6 ++++-- src/request.cpp | 9 +++++---- src/request.hpp | 12 ++---------- src/types.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 src/types.hpp diff --git a/src/hash.cpp b/src/hash.cpp index f211986..e1047c3 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -16,6 +16,8 @@ #include "hash.hpp" +#include "types.hpp" + #include #include #include @@ -30,14 +32,14 @@ namespace libravatarserv::hash { -std::string generate_hash(const algorithm algo, const std::string_view text) { +std::string generate_hash(const algorithm_t algo, const std::string_view text) { using namespace CryptoPP; SHA256 sha256; Weak::MD5 md5; - std::unique_ptr algo_to_use{&sha256}; - if (algo == algorithm::MD5) { - algo_to_use.reset(&md5); + HashTransformation *algo_to_use{&sha256}; // HashFilter calls free()? + if (algo == algorithm_t::MD5) { + algo_to_use = &md5; } std::string hash; @@ -49,18 +51,23 @@ std::string generate_hash(const algorithm algo, const std::string_view text) { return hash; } -table generate_table(const algorithm algo, const fs::path &directory) { - table hashtable; +std::string find_email(const request_t &req, const fs::path &directory) { + bool default_present{false}; for (const fs::path &path : fs::recursive_directory_iterator(directory)) { if (fs::is_regular_file(path)) { std::string email = path.filename(); std::transform(email.begin(), email.end(), email.begin(), ::tolower); - hashtable.insert({generate_hash(algo, email), email}); + if (generate_hash(req.algo, email) == req.hash) { + return email; + } + if (email == "default") { + default_present = true; + } } } - return hashtable; + return default_present ? "default" : ""; } bool is_valid(const std::string_view hash) { diff --git a/src/hash.hpp b/src/hash.hpp index cd47635..6f79b70 100644 --- a/src/hash.hpp +++ b/src/hash.hpp @@ -18,25 +18,20 @@ #define LIBRAVATARSERV_HASH_HPP #include "fs-compat.hpp" +#include "types.hpp" -#include #include #include namespace libravatarserv::hash { -enum class algorithm { - SHA256, - MD5 -}; - -using table = std::map; - // generate hash from text -[[nodiscard]] std::string generate_hash(algorithm algo, std::string_view text); +[[nodiscard]] std::string generate_hash(algorithm_t algo, + std::string_view text); -// generate hash -> email table -[[nodiscard]] table generate_table(algorithm algo, const fs::path &directory); +// Match hash to email +[[nodiscard]] std::string find_email(const request_t &req, + const fs::path &directory); // return false if invalid characters are found in hash [[nodiscard]] bool is_valid(std::string_view hash); diff --git a/src/main.cpp b/src/main.cpp index 661515d..7b583e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,9 +43,11 @@ int main(int /*argc*/, char * /*argv*/[]) { if (req.fallback.empty()) { req.fallback = cfg.default_fallback; } + const std::string email{hash::find_email(req, cfg.avatar_dir)}; + std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size - << "\n FALLBACK: " << req.fallback << '\n'; - const auto hashtable{hash::generate_table(req.algo, cfg.avatar_dir)}; + << "\n FALLBACK: " << req.fallback << "\n EMAIL: " << email + << '\n'; } catch (const std::runtime_error &e) { if (std::strlen(e.what()) != 0) { diff --git a/src/request.cpp b/src/request.cpp index 5fcb6fe..862b6e2 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -17,6 +17,7 @@ #include "request.hpp" #include "hash.hpp" +#include "types.hpp" #include #include @@ -27,7 +28,7 @@ namespace libravatarserv::request { -request parse(const std::string_view uri) { +request_t parse(const std::string_view uri) { constexpr std::uint8_t offset{8}; if (uri.substr(0, offset) != "/avatar/" || uri.find("..", offset) != std::string_view::npos) { @@ -35,7 +36,7 @@ request parse(const std::string_view uri) { throw std::runtime_error{"Invalid URL"}; } - request req; + request_t req; req.size = 80; size_t pos{uri.find('?')}; @@ -47,9 +48,9 @@ request parse(const std::string_view uri) { ::tolower); if (req.hash.size() == 64 && hash::is_valid(req.hash)) { - req.algo = hash::algorithm::SHA256; + req.algo = algorithm_t::SHA256; } else if (req.hash.size() == 32 && hash::is_valid(req.hash)) { - req.algo = hash::algorithm::MD5; + req.algo = algorithm_t::MD5; } else { std::cout << "Status: 400 Bad Request\n\n"; throw std::runtime_error{"Invalid hash"}; diff --git a/src/request.hpp b/src/request.hpp index 4394a90..57f2dfb 100644 --- a/src/request.hpp +++ b/src/request.hpp @@ -17,23 +17,15 @@ #ifndef LIBRAVATARSERV_REQUEST_HPP #define LIBRAVATARSERV_REQUEST_HPP -#include "hash.hpp" +#include "types.hpp" -#include #include #include namespace libravatarserv::request { -struct request { - std::string hash; - hash::algorithm algo; - std::int16_t size; - std::string fallback; -}; - // parse request and return results -[[nodiscard]] request parse(std::string_view uri); +[[nodiscard]] request_t parse(std::string_view uri); // get one parameter from an URI [[nodiscard]] std::string_view get_parameter(std::string_view uri, diff --git a/src/types.hpp b/src/types.hpp new file mode 100644 index 0000000..1347c4f --- /dev/null +++ b/src/types.hpp @@ -0,0 +1,38 @@ +/* This file is part of libravatarserv. + * Copyright © 2022 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBRAVATARSERV_TYPES_HPP +#define LIBRAVATARSERV_TYPES_HPP + +#include +#include +namespace libravatarserv { + +enum class algorithm_t { + SHA256, + MD5 +}; + +struct request_t { + std::string hash; + algorithm_t algo; + std::int16_t size; + std::string fallback; +}; + +} // namespace libravatarserv + +#endif // LIBRAVATARSERV_TYPES_HPP