Botan  1.11.15
src/lib/hash/par_hash/par_hash.cpp
Go to the documentation of this file.
00001 /*
00002 * Parallel Hash
00003 * (C) 1999-2009 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/hash_utils.h>
00009 #include <botan/par_hash.h>
00010 #include <botan/parsing.h>
00011 #include <botan/internal/algo_registry.h>
00012 
00013 namespace Botan {
00014 
00015 BOTAN_REGISTER_NAMED_T(HashFunction, "Parallel", Parallel, Parallel::make);
00016 
00017 Parallel* Parallel::make(const Spec& spec)
00018    {
00019    auto& hash_fns = Algo_Registry<HashFunction>::global_registry();
00020 
00021    std::vector<std::unique_ptr<HashFunction>> hashes;
00022 
00023    for(size_t i = 0; i != spec.arg_count(); ++i)
00024       {
00025       std::unique_ptr<HashFunction> h(hash_fns.make(spec.arg(i)));
00026 
00027       if(!h)
00028          return nullptr;
00029       hashes.push_back(std::move(h));
00030       }
00031 
00032    Parallel* p = new Parallel;
00033    std::swap(p->hashes, hashes);
00034    return p;
00035    }
00036 
00037 void Parallel::add_data(const byte input[], size_t length)
00038    {
00039    for(auto&& hash : hashes)
00040        hash->update(input, length);
00041    }
00042 
00043 void Parallel::final_result(byte out[])
00044    {
00045    u32bit offset = 0;
00046 
00047    for(auto&& hash : hashes)
00048       {
00049       hash->final(out + offset);
00050       offset += hash->output_length();
00051       }
00052    }
00053 
00054 size_t Parallel::output_length() const
00055    {
00056    size_t sum = 0;
00057 
00058    for(auto&& hash : hashes)
00059       sum += hash->output_length();
00060    return sum;
00061    }
00062 
00063 std::string Parallel::name() const
00064    {
00065    std::vector<std::string> names;
00066 
00067    for(auto&& hash : hashes)
00068       names.push_back(hash->name());
00069 
00070    return "Parallel(" + string_join(names, ',') + ")";
00071    }
00072 
00073 HashFunction* Parallel::clone() const
00074    {
00075    std::vector<HashFunction*> hash_copies;
00076 
00077    for(auto&& hash : hashes)
00078       hash_copies.push_back(hash->clone());
00079 
00080    return new Parallel(hash_copies);
00081    }
00082 
00083 void Parallel::clear()
00084    {
00085    for(auto&& hash : hashes)
00086       hash->clear();
00087    }
00088 
00089 Parallel::Parallel(const std::vector<HashFunction*>& in)
00090    {
00091    for(size_t i = 0; i != in.size(); ++i)
00092       {
00093       std::unique_ptr<HashFunction> h(in[i]->clone());
00094       hashes.push_back(std::move(h));
00095       }
00096    }
00097 
00098 
00099 }