OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / FileHash.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "FileHash.h"
24
25 //MUtils
26 #include <MUtils/Hash.h>
27 #include <MUtils/Exception.h>
28
29 static const char *g_blnk = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
30 static const char *g_seed = "c375d83b4388329408dfcbb4d9a065b6e06d28272f25ef299c70b506e26600af79fd2f866ae24602daf38f25c9d4b7e1";
31 static const char *g_salt = "ee9f7bdabc170763d2200a7e3030045aafe380011aefc1730e547e9244c62308aac42a976feeca224ba553de0c4bb883";
32
33 QByteArray FileHash::computeHash(QFile &file)
34 {
35         QByteArray result;
36
37         if(file.isOpen() && file.reset())
38         {
39                 QScopedPointer<MUtils::Hash::Hash> hash(MUtils::Hash::create(MUtils::Hash::HASH_KECCAK_384));
40                 const QByteArray data = file.readAll();
41                 if (data.size() >= 16)
42                 {
43                         const QByteArray seed = QByteArray::fromHex(g_seed);
44                         const QByteArray salt = QByteArray::fromHex(g_salt);
45
46                         bool okay[3];
47                         okay[0] = hash->update(seed);
48                         okay[1] = hash->update(data);
49                         okay[2] = hash->update(salt);
50
51                         if (okay[0] && okay[1] && okay[2])
52                         {
53                                 result = hash->digest(true);
54                         }
55                 }
56         }
57
58         return result.isEmpty() ? QByteArray::fromHex(g_blnk).toHex() : result;
59 }