Botan 3.8.1
Crypto and TLS for C&
buf_comp.h
Go to the documentation of this file.
1/*
2* Buffered Computation
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_BUFFERED_COMPUTATION_H_
9#define BOTAN_BUFFERED_COMPUTATION_H_
10
11#include <botan/concepts.h>
12#include <botan/secmem.h>
13#include <span>
14#include <string_view>
15
16namespace Botan {
17
18/**
19* This class represents any kind of computation which uses an internal
20* state, such as hash functions or MACs
21*/
23 public:
24 /**
25 * @return length of the output of this function in bytes
26 */
27 virtual size_t output_length() const = 0;
28
29 /**
30 * Add new input to process.
31 * @param in the input to process as a byte array
32 * @param length of param in in bytes
33 */
34 void update(const uint8_t in[], size_t length) { add_data({in, length}); }
35
36 /**
37 * Add new input to process.
38 * @param in the input to process as a contiguous data range
39 */
40 void update(std::span<const uint8_t> in) { add_data(in); }
41
42 void update_be(uint16_t val);
43 void update_be(uint32_t val);
44 void update_be(uint64_t val);
45
46 void update_le(uint16_t val);
47 void update_le(uint32_t val);
48 void update_le(uint64_t val);
49
50 /**
51 * Add new input to process.
52 * @param str the input to process as a std::string_view. Will be interpreted
53 * as a byte array based on the strings encoding.
54 */
55 void update(std::string_view str);
56
57 /**
58 * Process a single byte.
59 * @param in the byte to process
60 */
61 void update(uint8_t in) { add_data({&in, 1}); }
62
63 /**
64 * Complete the computation and retrieve the
65 * final result.
66 * @param out The byte array to be filled with the result.
67 * Must be of length output_length()
68 */
69 void final(uint8_t out[]) { final_result({out, output_length()}); }
70
71 /**
72 * Complete the computation and retrieve the
73 * final result as a container of your choice.
74 * @return a contiguous container holding the result
75 */
76 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
77 T final() {
78 T output(output_length());
79 final_result(output);
80 return output;
81 }
82
83 std::vector<uint8_t> final_stdvec() { return final<std::vector<uint8_t>>(); }
84
85 void final(std::span<uint8_t> out);
86
87 template <concepts::resizable_byte_buffer T>
88 void final(T& out) {
89 out.resize(output_length());
90 final_result(out);
91 }
92
93 /**
94 * Update and finalize computation. Does the same as calling update()
95 * and final() consecutively.
96 * @param in the input to process as a byte array
97 * @param length the length of the byte array
98 * @result the result of the call to final()
99 */
100 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
101 T process(const uint8_t in[], size_t length) {
102 update(in, length);
103 return final<T>();
104 }
105
106 /**
107 * Update and finalize computation. Does the same as calling update()
108 * and final() consecutively.
109 * @param in the input to process as a string
110 * @result the result of the call to final()
111 */
112 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
113 T process(std::string_view in) {
114 update(in);
115 return final<T>();
116 }
117
118 /**
119 * Update and finalize computation. Does the same as calling update()
120 * and final() consecutively.
121 * @param in the input to process as a contiguous container
122 * @result the result of the call to final()
123 */
124 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
125 T process(std::span<const uint8_t> in) {
126 update(in);
127 return final<T>();
128 }
129
130 virtual ~Buffered_Computation() = default;
131
132 private:
133 /**
134 * Add more data to the computation
135 * @param input is an input buffer
136 */
137 virtual void add_data(std::span<const uint8_t> input) = 0;
138
139 /**
140 * Write the final output to out
141 * @param out is an output buffer of output_length()
142 */
143 virtual void final_result(std::span<uint8_t> out) = 0;
144};
145
146} // namespace Botan
147
148#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
T process(std::string_view in)
Definition buf_comp.h:113
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:34
T process(std::span< const uint8_t > in)
Definition buf_comp.h:125
void update(std::span< const uint8_t > in)
Definition buf_comp.h:40
void update(uint8_t in)
Definition buf_comp.h:61
virtual size_t output_length() const =0
std::vector< uint8_t > final_stdvec()
Definition buf_comp.h:83
void final(uint8_t out[])
Definition buf_comp.h:69
virtual ~Buffered_Computation()=default
T process(const uint8_t in[], size_t length)
Definition buf_comp.h:101