Coverage Report

Created: 2024-06-28 06:19

/src/botan/build/include/public/botan/hash.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Hash Function Base Class
3
* (C) 1999-2008 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_HASH_FUNCTION_BASE_CLASS_H_
9
#define BOTAN_HASH_FUNCTION_BASE_CLASS_H_
10
11
#include <botan/buf_comp.h>
12
#include <memory>
13
#include <string>
14
#include <string_view>
15
16
namespace Botan {
17
18
/**
19
* This class represents hash function (message digest) objects
20
*/
21
class BOTAN_PUBLIC_API(2, 0) HashFunction : public Buffered_Computation {
22
   public:
23
      /**
24
      * Create an instance based on a name, or return null if the
25
      * algo/provider combination cannot be found. If provider is
26
      * empty then best available is chosen.
27
      */
28
      static std::unique_ptr<HashFunction> create(std::string_view algo_spec, std::string_view provider = "");
29
30
      /**
31
      * Create an instance based on a name
32
      * If provider is empty then best available is chosen.
33
      * @param algo_spec algorithm name
34
      * @param provider provider implementation to use
35
      * Throws Lookup_Error if not found.
36
      */
37
      static std::unique_ptr<HashFunction> create_or_throw(std::string_view algo_spec, std::string_view provider = "");
38
39
      /**
40
      * @return list of available providers for this algorithm, empty if not available
41
      * @param algo_spec algorithm name
42
      */
43
      static std::vector<std::string> providers(std::string_view algo_spec);
44
45
      /**
46
      * @return provider information about this implementation. Default is "base",
47
      * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
48
      */
49
0
      virtual std::string provider() const { return "base"; }
50
51
      ~HashFunction() override = default;
52
53
      /**
54
      * Reset the state.
55
      */
56
      virtual void clear() = 0;
57
58
      /**
59
      * @return the hash function name
60
      */
61
      virtual std::string name() const = 0;
62
63
      /**
64
      * @return hash block size as defined for this algorithm
65
      */
66
0
      virtual size_t hash_block_size() const { return 0; }
67
68
      /**
69
      * Return a new hash object with the same state as *this. This
70
      * allows computing the hash of several messages with a common
71
      * prefix more efficiently than would otherwise be possible.
72
      *
73
      * This function should be called `clone` but that was already
74
      * used for the case of returning an uninitialized object.
75
      * @return new hash object
76
      */
77
      virtual std::unique_ptr<HashFunction> copy_state() const = 0;
78
79
      /**
80
      * @return new object representing the same algorithm as *this
81
      */
82
      virtual std::unique_ptr<HashFunction> new_object() const = 0;
83
84
      /**
85
      * @return new object representing the same algorithm as *this
86
      */
87
0
      HashFunction* clone() const { return this->new_object().release(); }
88
};
89
90
}  // namespace Botan
91
92
#endif