ArnLib  4.0.x
Active Registry Network
ArnSyncLogin.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnSyncLogin.hpp"
33 #include "ArnInc/ArnLib.hpp"
34 #include <QCryptographicHash>
35 #include <QString>
36 #include <QDebug>
37 
38 
39 ArnSyncLogin::ArnSyncLogin()
40 {
41 }
42 
43 
44 void ArnSyncLogin::addAccess( const QString& userName, const QString& password, Arn::Allow allow)
45 {
46  AccessSlot accessSlot;
47  accessSlot.userName = userName;
48  accessSlot.pwHash = isPwHash( password) ? password : passwordHash( password);
49  accessSlot.allow = allow;
50 
51  _accessTab.insert( userName, accessSlot);
52 }
53 
54 
55 const ArnSyncLogin::AccessSlot* ArnSyncLogin::findAccess( const QString& userName)
56 {
57  if (!_accessTab.contains( userName)) return 0;
58 
59  return &_accessTab[ userName];
60 }
61 
62 
63 QByteArray ArnSyncLogin::pwHashXchg(uint saltA, uint saltB, const QString& password)
64 {
65  QByteArray pwSalted = password.toUtf8()
66  + "." + QByteArray::number( saltA, 16)
67  + "." + QByteArray::number( saltB, 16);
68 
69  QCryptographicHash hasher( QCryptographicHash::Sha1);
70  hasher.addData( pwSalted);
71  return hasher.result().toHex();
72 }
73 
74 
75 QString ArnSyncLogin::passwordHash( const QString& password)
76 {
77  QCryptographicHash hasher( QCryptographicHash::Sha1);
78  hasher.addData( password.toUtf8());
79  QByteArray pwHashed = hasher.result().toBase64();
80  pwHashed.chop(1); // Remove filler "="
81  pwHashed = "{" + pwHashed + "}";
82  return QString::fromLatin1( pwHashed.constData(), pwHashed.size());
83 }
84 
85 
86 bool ArnSyncLogin::isPwHash( const QString& password)
87 {
88  return password.startsWith("{") && password.endsWith("}");
89 }
90