Botan 3.4.0
Crypto and TLS for C&
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/internal/par_hash.h>
9
10#include <botan/internal/stl_util.h>
11
12#include <sstream>
13
14namespace Botan {
15
16void Parallel::add_data(std::span<const uint8_t> input) {
17 for(auto&& hash : m_hashes) {
18 hash->update(input);
19 }
20}
21
22void Parallel::final_result(std::span<uint8_t> output) {
23 BufferStuffer out(output);
24 for(auto&& hash : m_hashes) {
25 hash->final(out.next(hash->output_length()));
26 }
27}
28
30 size_t sum = 0;
31
32 for(auto&& hash : m_hashes) {
33 sum += hash->output_length();
34 }
35 return sum;
36}
37
38std::string Parallel::name() const {
39 std::ostringstream name;
40
41 name << "Parallel(";
42
43 for(size_t i = 0; i != m_hashes.size(); ++i) {
44 if(i != 0) {
45 name << ",";
46 }
47 name << m_hashes[i]->name();
48 }
49
50 name << ")";
51
52 return name.str();
53}
54
55std::unique_ptr<HashFunction> Parallel::new_object() const {
56 std::vector<std::unique_ptr<HashFunction>> hash_copies;
57 hash_copies.reserve(m_hashes.size());
58
59 for(auto&& hash : m_hashes) {
60 hash_copies.push_back(std::unique_ptr<HashFunction>(hash->new_object()));
61 }
62
63 return std::make_unique<Parallel>(hash_copies);
64}
65
66std::unique_ptr<HashFunction> Parallel::copy_state() const {
67 std::vector<std::unique_ptr<HashFunction>> hash_new_objects;
68 hash_new_objects.reserve(m_hashes.size());
69
70 for(const auto& hash : m_hashes) {
71 hash_new_objects.push_back(hash->copy_state());
72 }
73
74 return std::make_unique<Parallel>(hash_new_objects);
75}
76
78 for(auto&& hash : m_hashes) {
79 hash->clear();
80 }
81}
82
83Parallel::Parallel(std::vector<std::unique_ptr<HashFunction>>& hashes) {
84 m_hashes.reserve(hashes.size());
85 for(auto&& hash : hashes) {
86 m_hashes.push_back(std::move(hash));
87 }
88}
89
90} // namespace Botan
std::unique_ptr< HashFunction > new_object() const override
Definition par_hash.cpp:55
void clear() override
Definition par_hash.cpp:77
std::string name() const override
Definition par_hash.cpp:38
std::unique_ptr< HashFunction > copy_state() const override
Definition par_hash.cpp:66
Parallel(std::vector< std::unique_ptr< HashFunction > > &hashes)
Definition par_hash.cpp:83
size_t output_length() const override
Definition par_hash.cpp:29