Botan 3.13.0
Crypto and TLS for C&
hash.h
Go to the documentation of this file.
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
16namespace Botan {
17
18/**
19* This class represents hash function (message digest) objects
20*/
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 * List the providers available for a given hash
41 * @return list of available providers for this algorithm, empty if not available
42 * @param algo_spec algorithm name
43 */
44 static std::vector<std::string> providers(std::string_view algo_spec);
45
46 /**
47 * Return the name of the provider implementing this object
48 * @return provider information about this implementation. Default is "base",
49 * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
50 */
51 virtual std::string provider() const { return "base"; }
52
53 /**
54 * Reset the state.
55 */
56 virtual void clear() = 0;
57
58 /**
59 * Return the name of this hash function
60 * @return the hash function name
61 */
62 virtual std::string name() const = 0;
63
64 /**
65 * Return the internal block size of this hash function
66 * @return hash block size as defined for this algorithm
67 */
68 virtual size_t hash_block_size() const { return 0; }
69
70 /**
71 * Return an estimate, in bits, of the security level of this hash
72 * function, with respect to collision resistance. For most hashes this is
73 * simply half the output length, matching the generic birthday attack.
74 * It is lower for hash functions with a known collision attack, and zero
75 * for checksums and any hash where finding collisions is trivial.
76 */
77 virtual size_t security_level() const { return 4 * output_length(); }
78
79 /**
80 * Return a new hash object with the same state as *this. This
81 * allows computing the hash of several messages with a common
82 * prefix more efficiently than would otherwise be possible.
83 *
84 * This function should be called `clone` but that was already
85 * used for the case of returning an uninitialized object.
86 * @return new hash object
87 */
88 virtual std::unique_ptr<HashFunction> copy_state() const = 0;
89
90 /**
91 * Create a new uninitialized object of the same type
92 * @return new object representing the same algorithm as *this
93 */
94 virtual std::unique_ptr<HashFunction> new_object() const = 0;
95
96 /**
97 * Create a new uninitialized object of the same type
98 * @return new object representing the same algorithm as *this
99 */
100 HashFunction* clone() const { return this->new_object().release(); }
101};
102
103} // namespace Botan
104
105#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
virtual size_t output_length() const =0
static std::vector< std::string > providers(std::string_view algo_spec)
Definition hash.cpp:315
virtual std::string provider() const
Definition hash.h:51
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
virtual size_t security_level() const
Definition hash.h:77
HashFunction * clone() const
Definition hash.h:100
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:111
virtual size_t hash_block_size() const
Definition hash.h:68
virtual std::unique_ptr< HashFunction > copy_state() const =0
virtual std::unique_ptr< HashFunction > new_object() const =0
virtual std::string name() const =0
virtual void clear()=0