~singpolyma/biboumi

ref: ffb402f0adb9f808c7b8bc9616e71f7b3f8931ac biboumi/src/utils/sha1.cpp -rw-r--r-- 906 bytes
ffb402f0 — louiz’ Drop support for botan < 2.0 5 years ago
                                                                                
99a4dded louiz’
0ab40dc1 louiz’
99a4dded louiz’
28a537a9 louiz’
99a4dded louiz’
28a537a9 louiz’
99a4dded louiz’
e1a7114c Florent Le Coz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <utils/sha1.hpp>

#include <biboumi.h>

#ifdef BOTAN_FOUND
# include <botan/version.h>
# include <botan/hash.h>
# include <botan/hex.h>
# include <botan/exceptn.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
}