Botan  2.7.0
Crypto and TLS for C++11
par_hash.cpp
Go to the documentation of this file.
1 /*
2 * Parallel Hash
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/par_hash.h>
9 #include <botan/parsing.h>
10 
11 namespace Botan {
12 
13 void Parallel::add_data(const uint8_t input[], size_t length)
14  {
15  for(auto&& hash : m_hashes)
16  hash->update(input, length);
17  }
18 
19 void Parallel::final_result(uint8_t out[])
20  {
21  size_t offset = 0;
22 
23  for(auto&& hash : m_hashes)
24  {
25  hash->final(out + offset);
26  offset += hash->output_length();
27  }
28  }
29 
31  {
32  size_t sum = 0;
33 
34  for(auto&& hash : m_hashes)
35  sum += hash->output_length();
36  return sum;
37  }
38 
39 std::string Parallel::name() const
40  {
41  std::vector<std::string> names;
42 
43  for(auto&& hash : m_hashes)
44  names.push_back(hash->name());
45 
46  return "Parallel(" + string_join(names, ',') + ")";
47  }
48 
50  {
51  std::vector<std::unique_ptr<HashFunction>> hash_copies;
52 
53  for(auto&& hash : m_hashes)
54  hash_copies.push_back(std::unique_ptr<HashFunction>(hash->clone()));
55 
56  return new Parallel(hash_copies);
57  }
58 
59 std::unique_ptr<HashFunction> Parallel::copy_state() const
60  {
61  std::vector<std::unique_ptr<HashFunction>> hash_clones;
62 
63  for(const std::unique_ptr<HashFunction>& hash : m_hashes)
64  {
65  hash_clones.push_back(hash->copy_state());
66  }
67 
68  return std::unique_ptr<HashFunction>(new Parallel(hash_clones));
69  }
70 
72  {
73  for(auto&& hash : m_hashes)
74  hash->clear();
75  }
76 
77 Parallel::Parallel(std::vector<std::unique_ptr<HashFunction>>& h)
78  {
79  for(size_t i = 0; i != h.size(); ++i)
80  {
81  m_hashes.push_back(std::unique_ptr<HashFunction>(h[i].release()));
82  }
83  }
84 
85 
86 }
HashFunction * clone() const override
Definition: par_hash.cpp:49
std::unique_ptr< HashFunction > copy_state() const override
Definition: par_hash.cpp:59
size_t output_length() const override
Definition: par_hash.cpp:30
void clear() override
Definition: par_hash.cpp:71
Definition: alg_id.cpp:13
std::string name() const override
Definition: par_hash.cpp:39
MechanismType hash
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition: parsing.cpp:178