forked from tastytea/libravatarserv
Generate hashtable
This commit is contained in:
parent
03ae11a842
commit
cbd86c7000
|
@ -0,0 +1,80 @@
|
||||||
|
/* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "hash.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 // necessary for MD5
|
||||||
|
#include <cryptopp/cryptlib.h>
|
||||||
|
#include <cryptopp/filters.h>
|
||||||
|
#include <cryptopp/hex.h>
|
||||||
|
#include <cryptopp/md5.h>
|
||||||
|
#include <cryptopp/sha.h>
|
||||||
|
|
||||||
|
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<HashTransformation> 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
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* 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_HASH_HPP
|
||||||
|
#define LIBRAVATARSERV_HASH_HPP
|
||||||
|
|
||||||
|
#include "fs-compat.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);
|
||||||
|
|
||||||
|
// 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
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
#include "hash.hpp"
|
||||||
#include "helpers.hpp"
|
#include "helpers.hpp"
|
||||||
#include "request.hpp"
|
#include "request.hpp"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
|
@ -44,6 +45,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
||||||
}
|
}
|
||||||
std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size
|
std::cout << " HASH: " << req.hash << "\n SIZE: " << req.size
|
||||||
<< "\n FALLBACK: " << req.fallback << '\n';
|
<< "\n FALLBACK: " << req.fallback << '\n';
|
||||||
|
const auto hashtable{hash::generate_table(req.algo, cfg.avatar_dir)};
|
||||||
|
|
||||||
} catch (const std::runtime_error &e) {
|
} catch (const std::runtime_error &e) {
|
||||||
if (std::strlen(e.what()) != 0) {
|
if (std::strlen(e.what()) != 0) {
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
#include "request.hpp"
|
#include "request.hpp"
|
||||||
|
|
||||||
|
#include "hash.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -44,6 +46,15 @@ request parse(const std::string_view uri) {
|
||||||
std::transform(uri.begin() + offset, uri.begin() + pos, req.hash.begin(),
|
std::transform(uri.begin() + offset, uri.begin() + pos, req.hash.begin(),
|
||||||
::tolower);
|
::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()) {
|
if (pos != uri.size()) {
|
||||||
std::string uri_rest;
|
std::string uri_rest;
|
||||||
uri_rest.resize(uri.size() - pos);
|
uri_rest.resize(uri.size() - pos);
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
#ifndef LIBRAVATARSERV_REQUEST_HPP
|
#ifndef LIBRAVATARSERV_REQUEST_HPP
|
||||||
#define LIBRAVATARSERV_REQUEST_HPP
|
#define LIBRAVATARSERV_REQUEST_HPP
|
||||||
|
|
||||||
|
#include "hash.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
@ -25,12 +27,15 @@ namespace libravatarserv::request {
|
||||||
|
|
||||||
struct request {
|
struct request {
|
||||||
std::string hash;
|
std::string hash;
|
||||||
|
hash::algorithm algo;
|
||||||
std::int16_t size;
|
std::int16_t size;
|
||||||
std::string fallback;
|
std::string fallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// parse request and return results
|
||||||
[[nodiscard]] request parse(std::string_view uri);
|
[[nodiscard]] request parse(std::string_view uri);
|
||||||
|
|
||||||
|
// get one parameter from an URI
|
||||||
[[nodiscard]] std::string_view get_parameter(std::string_view uri,
|
[[nodiscard]] std::string_view get_parameter(std::string_view uri,
|
||||||
const std::string ¶meter);
|
const std::string ¶meter);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue