aboutsummaryrefslogtreecommitdiff
path: root/src/reducer/validation_DE_SVN.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reducer/validation_DE_SVN.c')
-rw-r--r--src/reducer/validation_DE_SVN.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/reducer/validation_DE_SVN.c b/src/reducer/validation_DE_SVN.c
new file mode 100644
index 0000000..bfc406d
--- /dev/null
+++ b/src/reducer/validation_DE_SVN.c
@@ -0,0 +1,98 @@
1/*
2 This file is part of Anastasis
3 Copyright (C) 2020, 2021 Taler Systems SA
4
5 Anastasis is free software; you can redistribute it and/or modify it under the
6 terms of the GNU Lesser General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file redux/validation_DE_SVN.c
18 * @brief
19 * @author Christian Grothoff
20 * @author Dominik Meister
21 * @author Dennis Neufeld
22 */
23#include <string.h>
24#include <stdbool.h>
25
26/**
27 * Sum up all digits in @a v and return the result.
28 */
29static unsigned int
30q (unsigned int v)
31{
32 unsigned int r = 0;
33
34 while (0 != v)
35 {
36 r += v % 10;
37 v = v / 10;
38 }
39 return r;
40}
41
42
43/**
44 * Function to validate a German Social Security number.
45 *
46 * See https://www.financescout24.de/wissen/ratgeber/sozialversicherungsnummer
47 * and https://de.wikipedia.org/wiki/Versicherungsnummer
48 * for the structure!
49 *
50 * @param avh_number ahv number to validate (input)
51 * @return true if validation passed, else false
52 */
53bool
54DE_SVN_check (const char *ssn_number)
55{
56 static const unsigned int factors[] = {
57 2, 1, 2, 5, 7, 1, 2, 1, 2, 1, 2, 1
58 };
59 unsigned int sum = 0;
60
61 if (strlen (ssn_number) != 12)
62 return false;
63 for (unsigned int i = 0; i<8; i++)
64 {
65 unsigned char c = (unsigned char) ssn_number[i];
66
67 if ( ('0' > c) || ('9' < c) )
68 return false;
69 sum += q ((c - '0') * factors[i]);
70 }
71 {
72 unsigned char c = (unsigned char) ssn_number[8];
73 unsigned int v = (c - 'A' + 1);
74
75 if ( ('A' > c) || ('Z' < c) )
76 return false;
77 sum += q ((v / 10) * factors[8]);
78 sum += q ((v % 10) * factors[9]);
79 }
80 for (unsigned int i = 9; i<11; i++)
81 {
82 unsigned char c = ssn_number[i];
83
84 if ( ('0' > c) || ('9' < c) )
85 return false;
86 sum += q ((c - '0') * factors[i + 1]);
87 }
88 if (ssn_number[11] != '0' + (sum % 10))
89 return false;
90 {
91 unsigned int month = (ssn_number[4] - '0') * 10 + (ssn_number[5] - '0');
92
93 if ( (0 == month) ||
94 (12 < month) )
95 return false;
96 }
97 return true;
98}