aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/hash/par_hash/par_hash.cpp
blob: 4971c023ce8a5b89404e604d4155ddce0cba51bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Parallel Hash
* (C) 1999-2009 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/par_hash.h>
#include <botan/internal/parsing.h>

namespace Botan {

void Parallel::add_data(const uint8_t input[], size_t length)
   {
   for(auto&& hash : m_hashes)
       hash->update(input, length);
   }

void Parallel::final_result(uint8_t out[])
   {
   size_t offset = 0;

   for(auto&& hash : m_hashes)
      {
      hash->final(out + offset);
      offset += hash->output_length();
      }
   }

size_t Parallel::output_length() const
   {
   size_t sum = 0;

   for(auto&& hash : m_hashes)
      sum += hash->output_length();
   return sum;
   }

std::string Parallel::name() const
   {
   std::vector<std::string> names;

   for(auto&& hash : m_hashes)
      names.push_back(hash->name());

   return "Parallel(" + string_join(names, ',') + ")";
   }

std::unique_ptr<HashFunction> Parallel::new_object() const
   {
   std::vector<std::unique_ptr<HashFunction>> hash_copies;

   for(auto&& hash : m_hashes)
      hash_copies.push_back(std::unique_ptr<HashFunction>(hash->new_object()));

   return std::make_unique<Parallel>(hash_copies);
   }

std::unique_ptr<HashFunction> Parallel::copy_state() const
   {
   std::vector<std::unique_ptr<HashFunction>> hash_new_objects;

   for(const auto& hash : m_hashes)
      {
      hash_new_objects.push_back(hash->copy_state());
      }

   return std::make_unique<Parallel>(hash_new_objects);
   }

void Parallel::clear()
   {
   for(auto&& hash : m_hashes)
      hash->clear();
   }

Parallel::Parallel(std::vector<std::unique_ptr<HashFunction>>& hashes)
   {
   for(auto&& hash: hashes)
      {
      m_hashes.push_back(std::move(hash));
      }
   }


}