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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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,150✔
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,391✔
115
      return nullptr;  // unknown provider
636,143✔
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

210
   const SCAN_Name req(algo_spec);
25,190✔
211

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

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

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

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

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

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

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

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

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

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

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

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

282
   return nullptr;
6,022✔
283
}
25,190✔
284

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

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

297
}
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

© 2025 Coveralls, Inc