forked from open-eid/qt-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIKValidator.cpp
121 lines (104 loc) · 2.86 KB
/
IKValidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* QEstEidCommon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "IKValidator.h"
#include <QtCore/QDate>
#include <QtCore/QStringList>
IKValidator::IKValidator( QObject *parent )
: QValidator( parent )
{}
QDate IKValidator::birthDate( const QString &ik )
{
if( ik.size() != 11 ) return QDate();
quint16 year = 0;
switch( ik.left( 1 ).toUInt() )
{
case 1: case 2: year = 1800; break;
case 3: case 4: year = 1900; break;
case 5: case 6: year = 2000; break;
case 7: case 8: year = 2100; break;
default: return QDate();
}
QDate date(
ik.mid( 1, 2 ).toUInt() + year,
ik.mid( 3, 2 ).toUInt(),
ik.mid( 5, 2 ).toUInt() );
return date.isValid() ? date : QDate();
}
bool IKValidator::isValid( const QString &ik )
{
if( ik.size() != 11 )
return false;
// Mobile-ID test IK-s
static const QStringList list = QStringList()
<< "14212128020"
<< "14212128021"
<< "14212128022"
<< "14212128023"
<< "14212128024"
<< "14212128025"
<< "14212128026"
<< "14212128027"
<< "38002240211"
<< "14212128029";
if( list.contains( ik ) )
return true;
// Validate date
if( birthDate( ik ).isNull() )
return false;
// Validate checksum
int sum1 = 0, sum2 = 0, pos1 = 1, pos2 = 3;
for( int i = 0; i < 10; ++i )
{
sum1 += ik.mid( i, 1 ).toUInt() * pos1;
sum2 += ik.mid( i, 1 ).toUInt() * pos2;
pos1 = pos1 == 9 ? 1 : pos1 + 1;
pos2 = pos2 == 9 ? 1 : pos2 + 1;
}
int result;
if( (result = sum1 % 11) >= 10 &&
(result = sum2 % 11) >= 10 )
result = 0;
return ik.right( 1 ).toInt() == result;
}
QValidator::State IKValidator::validate( QString &input, int & ) const
{
input = input.trimmed();
if( input.size() > 11 || !QRegExp( "\\d{0,11}" ).exactMatch( input ) )
return Invalid;
else if( input.size() == 11 )
return isValid( input ) ? Acceptable : Invalid;
else
return Intermediate;
}
NumberValidator::NumberValidator( QObject *parent )
: QValidator( parent )
{}
NumberValidator::State NumberValidator::validate( QString &input, int & ) const
{
QString out;
QRegExp rx( "(\\d+)" );
int p = 0;
while( (p = rx.indexIn( input, p )) != -1 )
{
out += input.mid( p, rx.matchedLength() );
p += rx.matchedLength();
}
input = out;
return Acceptable;
}