Skip to content

Commit

Permalink
Ready for adding other hash algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetrwn committed Nov 15, 2023
1 parent 7f7f54c commit b396f57
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HashTools {
public:
static std::string md5(const std::string &src);
static std::string digest(const std::string &algorithm, const std::string &src);
};

#endif
2 changes: 1 addition & 1 deletion src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ActionTransform::transform(const std::vector<std::string> &input, const Actions
break;
case MD5:
output.reserve(1);
hexCiphertext = HashTools::md5(input[0]);
hexCiphertext = HashTools::digest("md5", input[0]);
output.emplace_back(std::pair<std::string, std::string>("Hex Hash", hexCiphertext));
break;
default:
Expand Down
10 changes: 6 additions & 4 deletions src/hash.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "hash.hpp"

std::string HashTools::md5(const std::string &src) {
std::string HashTools::digest(const std::string &algorithm, const std::string &src) {
EVP_MD_CTX *mdctx;
const EVP_MD *md;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int mdLen;

OpenSSL_add_all_digests();
md = EVP_get_digestbyname("md5");
md = EVP_get_digestbyname(algorithm.c_str());

if (!md)
throw std::runtime_error("Unable to initialize HashTools::md5 digest.");
if (!md) {
std::string msg = "Unable to find digest for algorithm " + algorithm + ".";
throw std::runtime_error(msg);
}

mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, md, NULL);
Expand Down

0 comments on commit b396f57

Please sign in to comment.