OSDN Git Service

IPCChannel: Trim parameter strings before sending.
[mutilities/MUtilities.git] / include / MUtils / KeccakHash.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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.
14 //
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
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
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 ****************************************************************************/
46
47 #pragma once
48
49 //MUtils
50 #include <MUtils/Global.h>
51
52 //Qt
53 #include <QString>
54 #include <QByteArray>
55 #include <QFile>
56
57 namespace MUtils
58 {
59         namespace Internal
60         {
61                 // Section from KeccakSponge.h
62                 // needed here, since hashState needs to be explicitly 32-byte aligned and therefore can't be
63                 // transformed into a class (in order to forward declarate) like in the other hash wrappers.
64                 namespace KeccakImpl
65                 {
66                         #define KeccakPermutationSize 1600
67                         #define KeccakPermutationSizeInBytes (KeccakPermutationSize/8)
68                         #define KeccakMaximumRate 1536
69                         #define KeccakMaximumRateInBytes (KeccakMaximumRate/8)
70
71                         #if defined(__GNUC__)
72                         #define ALIGN __attribute__ ((aligned(32)))
73                         #elif defined(_MSC_VER)
74                         #define ALIGN __declspec(align(32))
75                         #else
76                         #define ALIGN
77                         #endif
78
79                         ALIGN typedef struct spongeStateStruct
80                         {
81                                 ALIGN unsigned char state[KeccakPermutationSizeInBytes];
82                                 ALIGN unsigned char dataQueue[KeccakMaximumRateInBytes];
83                                 unsigned int rate;
84                                 unsigned int capacity;
85                                 unsigned int bitsInQueue;
86                                 unsigned int fixedOutputLength;
87                                 int squeezing;
88                                 unsigned int bitsAvailableForSqueezing;
89                         }
90                         spongeState;
91                         typedef spongeState hashState;
92                 }
93                 // End Section from KeccakSponge.h
94         }
95
96         class MUTILS_API KeccakHash
97         {
98         public:
99                 enum HashBits {hb224, hb256, hb384, hb512};
100                 
101                 KeccakHash();
102                 ~KeccakHash();
103                 
104                 static bool selfTest(void);
105
106                 bool init(HashBits hashBits=hb256);
107                 bool addData(const QByteArray &data);
108                 bool addData(const char *data, int size);
109                 const QByteArray &finalize();
110
111         protected:
112                 bool m_initialized;
113                 Internal::KeccakImpl::hashState *m_state;
114                 QByteArray m_hashResult;
115         };
116 };