Some code refactoring.
[MUtilities.git] / src / Hash_Keccak.h
blobe9eb4dc84c5f72a6606a70896d9af6cb912b7dae
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 /***************************************************************************
23 ** **
24 ** QKeccakHash, an API wrapper bringing the optimized implementation of **
25 ** Keccak (http://keccak.noekeon.org/) to Qt. **
26 ** Copyright (C) 2013 Emanuel Eichhammer **
27 ** **
28 ** This program is free software: you can redistribute it and/or modify **
29 ** it under the terms of the GNU General Public License as published by **
30 ** the Free Software Foundation, either version 3 of the License, or **
31 ** (at your option) any later version. **
32 ** **
33 ** This program is distributed in the hope that it will be useful, **
34 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
35 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
36 ** GNU General Public License for more details. **
37 ** **
38 ** You should have received a copy of the GNU General Public License **
39 ** along with this program. If not, see http://www.gnu.org/licenses/. **
40 ** **
41 ****************************************************************************
42 ** Author: Emanuel Eichhammer **
43 ** Website/Contact: http://www.WorksLikeClockwork.com/ **
44 ** Date: 12.01.12 **
45 ****************************************************************************/
47 #pragma once
49 //MUtils
50 #include <MUtils/Global.h>
51 #include <MUtils/Hash.h>
53 //Qt
54 #include <QString>
55 #include <QByteArray>
56 #include <QFile>
58 namespace MUtils
60 namespace Hash
62 namespace Internal
64 // Section from KeccakSponge.h
65 // needed here, since hashState needs to be explicitly 32-byte aligned and therefore can't be
66 // transformed into a class (in order to forward declarate) like in the other hash wrappers.
67 namespace KeccakImpl
69 #define KeccakPermutationSize 1600
70 #define KeccakPermutationSizeInBytes (KeccakPermutationSize/8)
71 #define KeccakMaximumRate 1536
72 #define KeccakMaximumRateInBytes (KeccakMaximumRate/8)
74 #if defined(__GNUC__)
75 #define ALIGN __attribute__ ((aligned(32)))
76 #elif defined(_MSC_VER)
77 #define ALIGN __declspec(align(32))
78 #else
79 #define ALIGN
80 #endif
82 ALIGN typedef struct spongeStateStruct
84 ALIGN unsigned char state[KeccakPermutationSizeInBytes];
85 ALIGN unsigned char dataQueue[KeccakMaximumRateInBytes];
86 unsigned int rate;
87 unsigned int capacity;
88 unsigned int bitsInQueue;
89 unsigned int fixedOutputLength;
90 int squeezing;
91 unsigned int bitsAvailableForSqueezing;
93 spongeState;
94 typedef spongeState hashState;
96 // End Section from KeccakSponge.h
99 class MUTILS_API Keccak : public Hash
101 public:
102 enum HashBits {hb224, hb256, hb384, hb512};
104 Keccak();
105 virtual ~Keccak();
107 bool init(const HashBits hashBits=hb256);
109 static Keccak *create(const HashBits hashBits = hb256, const char *const key = NULL);
110 static bool selfTest(void);
112 protected:
113 bool m_initialized;
114 Internal::KeccakImpl::hashState *m_state;
116 virtual bool process(const quint8 *const data, const quint32 len);
117 virtual QByteArray finalize(void);