move types to types.hpp, match hash to email

This commit is contained in:
tastytea 2022-06-11 03:37:09 +02:00
parent cbd86c7000
commit cb354e666d
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
6 changed files with 70 additions and 35 deletions

View File

@ -16,6 +16,8 @@
#include "hash.hpp"
#include "types.hpp"
#include <algorithm>
#include <memory>
#include <string>
@ -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<HashTransformation> 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) {

View File

@ -18,25 +18,20 @@
#define LIBRAVATARSERV_HASH_HPP
#include "fs-compat.hpp"
#include "types.hpp"
#include <map>
#include <string>
#include <string_view>
namespace libravatarserv::hash {
enum class algorithm {
SHA256,
MD5
};
using table = std::map<std::string, std::string>;
// 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);

View File

@ -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) {

View File

@ -17,6 +17,7 @@
#include "request.hpp"
#include "hash.hpp"
#include "types.hpp"
#include <algorithm>
#include <cstdint>
@ -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"};

View File

@ -17,23 +17,15 @@
#ifndef LIBRAVATARSERV_REQUEST_HPP
#define LIBRAVATARSERV_REQUEST_HPP
#include "hash.hpp"
#include "types.hpp"
#include <cstdint>
#include <string>
#include <string_view>
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,

38
src/types.hpp Normal file
View File

@ -0,0 +1,38 @@
/* This file is part of libravatarserv.
* Copyright © 2022 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef LIBRAVATARSERV_TYPES_HPP
#define LIBRAVATARSERV_TYPES_HPP
#include <cstdint>
#include <string>
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