1 /**
2 * Parallel Hash
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.hash.par_hash;
12 
13 import botan.constants;
14 static if (BOTAN_HAS_PARALLEL_HASH):
15 
16 import botan.hash.hash;
17 import botan.utils.types;
18 import botan.utils.parsing;
19 
20 /**
21 * Parallel Hashes
22 */
23 class Parallel : HashFunction
24 {
25 public:
26 
27     override @property size_t hashBlockSize() const { return 0; }
28 
29     /*
30     * Clear memory of sensitive data
31     */
32     override void clear()
33     {
34         foreach (hash; m_hashes[])
35             hash.clear();
36     }
37 
38     /*
39     * Return the name of this type
40     */
41     override @property string name() const
42     {
43         Vector!string names;
44         
45         foreach (hash; m_hashes[])
46             names.pushBack(hash.name);
47         
48         return "Parallel(" ~ stringJoin(names, ',') ~ ")";
49     }
50 
51     /*
52     * Return a clone of this object
53     */
54     override HashFunction clone() const
55     {
56         Vector!HashFunction hash_copies;
57         
58         foreach (hash; m_hashes[])
59             hash_copies.pushBack(hash.clone());
60         
61         return new Parallel(hash_copies.move());
62     }
63 
64     /*
65     * Return output size
66     */
67     override @property size_t outputLength() const
68     {
69         size_t sum = 0;
70         
71         foreach (hash; m_hashes[])
72             sum += hash.outputLength;
73         return sum;
74     }
75 
76     /**
77     * Constructor
78     * Params:
79     *  hash_input = a set of hashes to compute in parallel
80     */
81     this()(auto ref Vector!HashFunction hash_input)
82     {
83         m_hashes = hash_input.move();
84     }
85 
86     /*
87     * Parallel Destructor
88     */
89     ~this()
90     {
91         foreach (hash; m_hashes[])
92             destroy(hash);
93     }
94 protected:
95     /*
96     * Update the hash
97     */
98     override void addData(const(ubyte)* input, size_t length)
99     {
100         foreach (hash; m_hashes[])
101             hash.update(input, length);
102     }
103 
104     /*
105     * Finalize the hash
106     */
107     override void finalResult(ubyte* output)
108     {
109         uint offset = 0;
110         
111         foreach (hash; m_hashes[])
112         {
113             hash.flushInto(output + offset);
114             offset += hash.outputLength;
115         }
116     }
117 
118     Vector!HashFunction m_hashes;
119 }