• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.77
/src/lib/hash/hash.cpp
1
/*
2
* Hash Functions
3
* (C) 2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/hash.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/scan_name.h>
12

13
#if defined(BOTAN_HAS_ADLER32)
14
   #include <botan/internal/adler32.h>
15
#endif
16

17
#if defined(BOTAN_HAS_CRC24)
18
   #include <botan/internal/crc24.h>
19
#endif
20

21
#if defined(BOTAN_HAS_CRC32)
22
   #include <botan/internal/crc32.h>
23
#endif
24

25
#if defined(BOTAN_HAS_GOST_34_11)
26
   #include <botan/internal/gost_3411.h>
27
#endif
28

29
#if defined(BOTAN_HAS_KECCAK)
30
   #include <botan/internal/keccak.h>
31
#endif
32

33
#if defined(BOTAN_HAS_MD4)
34
   #include <botan/internal/md4.h>
35
#endif
36

37
#if defined(BOTAN_HAS_MD5)
38
   #include <botan/internal/md5.h>
39
#endif
40

41
#if defined(BOTAN_HAS_RIPEMD_160)
42
   #include <botan/internal/rmd160.h>
43
#endif
44

45
#if defined(BOTAN_HAS_SHA1)
46
   #include <botan/internal/sha1.h>
47
#endif
48

49
#if defined(BOTAN_HAS_SHA2_32)
50
   #include <botan/internal/sha2_32.h>
51
#endif
52

53
#if defined(BOTAN_HAS_SHA2_64)
54
   #include <botan/internal/sha2_64.h>
55
#endif
56

57
#if defined(BOTAN_HAS_SHA3)
58
   #include <botan/internal/sha3.h>
59
#endif
60

61
#if defined(BOTAN_HAS_SHAKE)
62
   #include <botan/internal/shake.h>
63
#endif
64

65
#if defined(BOTAN_HAS_SKEIN_512)
66
   #include <botan/internal/skein_512.h>
67
#endif
68

69
#if defined(BOTAN_HAS_STREEBOG)
70
   #include <botan/internal/streebog.h>
71
#endif
72

73
#if defined(BOTAN_HAS_SM3)
74
   #include <botan/internal/sm3.h>
75
#endif
76

77
#if defined(BOTAN_HAS_WHIRLPOOL)
78
   #include <botan/internal/whrlpool.h>
79
#endif
80

81
#if defined(BOTAN_HAS_PARALLEL_HASH)
82
   #include <botan/internal/par_hash.h>
83
#endif
84

85
#if defined(BOTAN_HAS_TRUNCATED_HASH)
86
   #include <botan/internal/trunc_hash.h>
87
#endif
88

89
#if defined(BOTAN_HAS_COMB4P)
90
   #include <botan/internal/comb4p.h>
91
#endif
92

93
#if defined(BOTAN_HAS_BLAKE2B)
94
   #include <botan/internal/blake2b.h>
95
#endif
96

97
#if defined(BOTAN_HAS_COMMONCRYPTO)
98
   #include <botan/internal/commoncrypto.h>
99
#endif
100

101
namespace Botan {
102

103
std::unique_ptr<HashFunction> HashFunction::create(std::string_view algo_spec, std::string_view provider) {
636,136✔
104
#if defined(BOTAN_HAS_COMMONCRYPTO)
105
   if(provider.empty() || provider == "commoncrypto") {
106
      if(auto hash = make_commoncrypto_hash(algo_spec))
107
         return hash;
108

109
      if(!provider.empty())
110
         return nullptr;
111
   }
112
#endif
113

114
   if(provider.empty() == false && provider != "base") {
890,377✔
115
      return nullptr;  // unknown provider
636,129✔
116
   }
117

118
#if defined(BOTAN_HAS_SHA1)
119
   if(algo_spec == "SHA-1") {
388,222✔
120
      return std::make_unique<SHA_1>();
102,944✔
121
   }
122
#endif
123

124
#if defined(BOTAN_HAS_SHA2_32)
125
   if(algo_spec == "SHA-224") {
541,250✔
126
      return std::make_unique<SHA_224>();
1,539✔
127
   }
128

129
   if(algo_spec == "SHA-256") {
345,502✔
130
      return std::make_unique<SHA_256>();
194,209✔
131
   }
132
#endif
133

134
#if defined(BOTAN_HAS_SHA2_64)
135
   if(algo_spec == "SHA-384") {
97,774✔
136
      return std::make_unique<SHA_384>();
53,519✔
137
   }
138

139
   if(algo_spec == "SHA-512") {
37,071✔
140
      return std::make_unique<SHA_512>();
7,184✔
141
   }
142

143
   if(algo_spec == "SHA-512-256") {
29,151✔
144
      return std::make_unique<SHA_512_256>();
1,829✔
145
   }
146
#endif
147

148
#if defined(BOTAN_HAS_RIPEMD_160)
149
   if(algo_spec == "RIPEMD-160") {
33,645✔
150
      return std::make_unique<RIPEMD_160>();
193✔
151
   }
152
#endif
153

154
#if defined(BOTAN_HAS_WHIRLPOOL)
155
   if(algo_spec == "Whirlpool") {
26,675✔
156
      return std::make_unique<Whirlpool>();
24✔
157
   }
158
#endif
159

160
#if defined(BOTAN_HAS_MD5)
161
   if(algo_spec == "MD5") {
27,184✔
162
      return std::make_unique<MD5>();
240✔
163
   }
164
#endif
165

166
#if defined(BOTAN_HAS_MD4)
167
   if(algo_spec == "MD4") {
26,788✔
168
      return std::make_unique<MD4>();
156✔
169
   }
170
#endif
171

172
#if defined(BOTAN_HAS_GOST_34_11)
173
   if(algo_spec == "GOST-R-34.11-94" || algo_spec == "GOST-34.11") {
33,150✔
174
      return std::make_unique<GOST_34_11>();
22✔
175
   }
176
#endif
177

178
#if defined(BOTAN_HAS_ADLER32)
179
   if(algo_spec == "Adler32") {
27,361✔
180
      return std::make_unique<Adler32>();
62✔
181
   }
182
#endif
183

184
#if defined(BOTAN_HAS_CRC24)
185
   if(algo_spec == "CRC24") {
26,235✔
186
      return std::make_unique<CRC24>();
66✔
187
   }
188
#endif
189

190
#if defined(BOTAN_HAS_CRC32)
191
   if(algo_spec == "CRC32") {
26,105✔
192
      return std::make_unique<CRC32>();
64✔
193
   }
194
#endif
195

196
#if defined(BOTAN_HAS_STREEBOG)
197
   if(algo_spec == "Streebog-256") {
30,357✔
198
      return std::make_unique<Streebog>(256);
270✔
199
   }
200
   if(algo_spec == "Streebog-512") {
29,821✔
201
      return std::make_unique<Streebog>(512);
266✔
202
   }
203
#endif
204

205
#if defined(BOTAN_HAS_SM3)
206
   if(algo_spec == "SM3") {
25,554✔
207
      return std::make_unique<SM3>();
328✔
208
   }
209
#endif
210

211
   const SCAN_Name req(algo_spec);
25,175✔
212

213
#if defined(BOTAN_HAS_SKEIN_512)
214
   if(req.algo_name() == "Skein-512") {
50,351✔
215
      return std::make_unique<Skein_512>(req.arg_as_integer(0, 512), req.arg(1, ""));
2,074✔
216
   }
217
#endif
218

219
#if defined(BOTAN_HAS_BLAKE2B)
220
   if(req.algo_name() == "Blake2b" || req.algo_name() == "BLAKE2b") {
92,403✔
221
      return std::make_unique<BLAKE2b>(req.arg_as_integer(0, 512));
5,182✔
222
   }
223
#endif
224

225
#if defined(BOTAN_HAS_KECCAK)
226
   if(req.algo_name() == "Keccak-1600") {
35,839✔
227
      return std::make_unique<Keccak_1600>(req.arg_as_integer(0, 512));
2,145✔
228
   }
229
#endif
230

231
#if defined(BOTAN_HAS_SHA3)
232
   if(req.algo_name() == "SHA-3") {
31,549✔
233
      return std::make_unique<SHA_3>(req.arg_as_integer(0, 512));
6,769✔
234
   }
235
#endif
236

237
#if defined(BOTAN_HAS_SHAKE)
238
   if(req.algo_name() == "SHAKE-128" && req.arg_count() == 1) {
27,015✔
239
      return std::make_unique<SHAKE_128>(req.arg_as_integer(0));
634✔
240
   }
241
   if(req.algo_name() == "SHAKE-256" && req.arg_count() == 1) {
25,113✔
242
      return std::make_unique<SHAKE_256>(req.arg_as_integer(0));
2,263✔
243
   }
244
#endif
245

246
#if defined(BOTAN_HAS_PARALLEL_HASH)
247
   if(req.algo_name() == "Parallel") {
12,217✔
248
      std::vector<std::unique_ptr<HashFunction>> hashes;
8✔
249

250
      for(size_t i = 0; i != req.arg_count(); ++i) {
24✔
251
         auto h = HashFunction::create(req.arg(i));
16✔
252
         if(!h) {
16✔
253
            return nullptr;
×
254
         }
255
         hashes.push_back(std::move(h));
16✔
256
      }
16✔
257

258
      return std::make_unique<Parallel>(hashes);
8✔
259
   }
8✔
260
#endif
261

262
#if defined(BOTAN_HAS_TRUNCATED_HASH)
263
   if(req.algo_name() == "Truncated" && req.arg_count() == 2) {
18,300✔
264
      auto hash = HashFunction::create(req.arg(0));
69✔
265
      if(!hash) {
69✔
266
         return nullptr;
67✔
267
      }
268

269
      return std::make_unique<Truncated_Hash>(std::move(hash), req.arg_as_integer(1));
68✔
270
   }
69✔
271
#endif
272

273
#if defined(BOTAN_HAS_COMB4P)
274
   if(req.algo_name() == "Comb4P" && req.arg_count() == 2) {
12,068✔
275
      auto h1 = HashFunction::create(req.arg(0));
6✔
276
      auto h2 = HashFunction::create(req.arg(1));
8✔
277

278
      if(h1 && h2) {
6✔
279
         return std::make_unique<Comb4P>(std::move(h1), std::move(h2));
6✔
280
      }
281
   }
8✔
282
#endif
283

284
   return nullptr;
6,025✔
285
}
25,175✔
286

287
//static
288
std::unique_ptr<HashFunction> HashFunction::create_or_throw(std::string_view algo, std::string_view provider) {
60,864✔
289
   if(auto hash = HashFunction::create(algo, provider)) {
60,864✔
290
      return hash;
60,858✔
291
   }
60,858✔
292
   throw Lookup_Error("Hash", algo, provider);
1✔
293
}
294

295
std::vector<std::string> HashFunction::providers(std::string_view algo_spec) {
248,046✔
296
   return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
744,138✔
297
}
298

299
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc