Botan 3.13.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/exceptn.h>
11#include <botan/internal/buffer_stuffer.h>
12#include <algorithm>
13#include <sstream>
14
15namespace Botan {
16
17void Parallel::add_data(std::span<const uint8_t> input) {
18 for(auto&& hash : m_hashes) {
19 hash->update(input);
20 }
21}
22
23void Parallel::final_result(std::span<uint8_t> output) {
24 BufferStuffer out(output);
25 for(auto&& hash : m_hashes) {
26 hash->final(out.next(hash->output_length()));
27 }
28}
29
31 size_t sum = 0;
32
33 for(auto&& hash : m_hashes) {
34 sum += hash->output_length();
35 }
36 return sum;
37}
38
40 // Joux multicollisions show a concatenation is barely stronger than its strongest hash
41 size_t level = 0;
42
43 for(auto&& hash : m_hashes) {
44 level = std::max(level, hash->security_level());
45 }
46 return level;
47}
48
49std::string Parallel::name() const {
50 std::ostringstream name;
51
52 name << "Parallel(";
53
54 for(size_t i = 0; i != m_hashes.size(); ++i) {
55 if(i != 0) {
56 name << ",";
57 }
58 name << m_hashes[i]->name();
59 }
60
61 name << ")";
62
63 return name.str();
64}
65
66std::unique_ptr<HashFunction> Parallel::new_object() const {
67 std::vector<std::unique_ptr<HashFunction>> hash_copies;
68 hash_copies.reserve(m_hashes.size());
69
70 for(auto&& hash : m_hashes) {
71 hash_copies.push_back(std::unique_ptr<HashFunction>(hash->new_object()));
72 }
73
74 return std::make_unique<Parallel>(hash_copies);
75}
76
77std::unique_ptr<HashFunction> Parallel::copy_state() const {
78 std::vector<std::unique_ptr<HashFunction>> hash_new_objects;
79 hash_new_objects.reserve(m_hashes.size());
80
81 for(const auto& hash : m_hashes) {
82 hash_new_objects.push_back(hash->copy_state());
83 }
84
85 return std::make_unique<Parallel>(hash_new_objects);
86}
87
89 for(auto&& hash : m_hashes) {
90 hash->clear();
91 }
92}
93
94Parallel::Parallel(std::vector<std::unique_ptr<HashFunction>>& hashes) {
95 if(hashes.size() < 2) {
96 throw Invalid_Argument("Parallel hash requires at least two hashes be specified");
97 }
98 m_hashes.reserve(hashes.size());
99 for(auto&& hash : hashes) {
100 m_hashes.push_back(std::move(hash));
101 }
102}
103
104} // namespace Botan
std::unique_ptr< HashFunction > new_object() const override
Definition par_hash.cpp:66
void clear() override
Definition par_hash.cpp:88
std::string name() const override
Definition par_hash.cpp:49
std::unique_ptr< HashFunction > copy_state() const override
Definition par_hash.cpp:77
Parallel(std::vector< std::unique_ptr< HashFunction > > &hashes)
Definition par_hash.cpp:94
size_t security_level() const override
Definition par_hash.cpp:39
size_t output_length() const override
Definition par_hash.cpp:30