This adds a hard dependency on one of Botan or gcrypt. Botan is already a recommended dependency, and gcrypt is probably packaged almost everywhere, so this should not be a big deal. ref #3241
33 lines
853 B
C++
33 lines
853 B
C++
#include <utils/sha1.hpp>
|
|
|
|
#include <louloulibs.h>
|
|
|
|
#ifdef BOTAN_FOUND
|
|
# include <botan/hash.h>
|
|
# include <botan/hex.h>
|
|
#endif
|
|
#ifdef GCRYPT_FOUND
|
|
# include <gcrypt.h>
|
|
# include <vector>
|
|
# include <iomanip>
|
|
# include <sstream>
|
|
#endif
|
|
|
|
std::string sha1(const std::string& input)
|
|
{
|
|
#ifdef BOTAN_FOUND
|
|
auto sha1 = Botan::HashFunction::create_or_throw("SHA-1");
|
|
sha1->update(input);
|
|
return Botan::hex_encode(sha1->final(), false);
|
|
#endif
|
|
#ifdef GCRYPT_FOUND
|
|
const auto hash_length = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
|
|
std::vector<uint8_t> output(hash_length, {});
|
|
gcry_md_hash_buffer(GCRY_MD_SHA1, output.data(), input.data(), input.size());
|
|
std::ostringstream digest;
|
|
for (std::size_t i = 0; i < hash_length; i++)
|
|
digest << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(output[i]);
|
|
return digest.str();
|
|
#endif
|
|
}
|