diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..f211986 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,80 @@ +/* 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 . + */ + +#include "hash.hpp" + +#include +#include +#include +#include + +#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 // necessary for MD5 +#include +#include +#include +#include +#include + +namespace libravatarserv::hash { + +std::string generate_hash(const algorithm 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); + } + + std::string hash; + StringSource s(text.data(), true, + new HashFilter(*algo_to_use, + new HexEncoder(new StringSink(hash)))); + std::transform(hash.begin(), hash.end(), hash.begin(), ::tolower); + + return hash; +} + +table generate_table(const algorithm algo, const fs::path &directory) { + table hashtable; + 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}); + } + } + + return hashtable; +} + +bool is_valid(const std::string_view hash) { + auto not_hex{[](const char c) { + if (c >= 0x61 && c <= 0x66) { // a-f + return false; + } + if (c >= 0x30 && c <= 0x39) { // 0-9 + return false; + } + return true; + }}; + + return !std::any_of(hash.begin(), hash.end(), not_hex); +} + +} // namespace libravatarserv::hash diff --git a/src/hash.hpp b/src/hash.hpp new file mode 100644 index 0000000..cd47635 --- /dev/null +++ b/src/hash.hpp @@ -0,0 +1,46 @@ +/* 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_HASH_HPP +#define LIBRAVATARSERV_HASH_HPP + +#include "fs-compat.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); + +// generate hash -> email table +[[nodiscard]] table generate_table(algorithm algo, const fs::path &directory); + +// return false if invalid characters are found in hash +[[nodiscard]] bool is_valid(std::string_view hash); + +} // namespace libravatarserv::hash + +#endif // LIBRAVATARSERV_HASH_HPP diff --git a/src/main.cpp b/src/main.cpp index 071c143..661515d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ */ #include "config.hpp" +#include "hash.hpp" #include "helpers.hpp" #include "request.hpp" #include "version.hpp" @@ -44,6 +45,7 @@ int main(int /*argc*/, char * /*argv*/[]) { } 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)}; } catch (const std::runtime_error &e) { if (std::strlen(e.what()) != 0) { diff --git a/src/request.cpp b/src/request.cpp index 12eb073..5fcb6fe 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -16,6 +16,8 @@ #include "request.hpp" +#include "hash.hpp" + #include #include #include @@ -44,6 +46,15 @@ request parse(const std::string_view uri) { std::transform(uri.begin() + offset, uri.begin() + pos, req.hash.begin(), ::tolower); + if (req.hash.size() == 64 && hash::is_valid(req.hash)) { + req.algo = hash::algorithm::SHA256; + } else if (req.hash.size() == 32 && hash::is_valid(req.hash)) { + req.algo = hash::algorithm::MD5; + } else { + std::cout << "Status: 400 Bad Request\n\n"; + throw std::runtime_error{"Invalid hash"}; + } + if (pos != uri.size()) { std::string uri_rest; uri_rest.resize(uri.size() - pos); diff --git a/src/request.hpp b/src/request.hpp index 1b868cc..4394a90 100644 --- a/src/request.hpp +++ b/src/request.hpp @@ -17,6 +17,8 @@ #ifndef LIBRAVATARSERV_REQUEST_HPP #define LIBRAVATARSERV_REQUEST_HPP +#include "hash.hpp" + #include #include #include @@ -25,12 +27,15 @@ 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); +// get one parameter from an URI [[nodiscard]] std::string_view get_parameter(std::string_view uri, const std::string ¶meter);