-
Notifications
You must be signed in to change notification settings - Fork 26
/
pwnr.ts
157 lines (136 loc) · 4.02 KB
/
pwnr.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* PwNr (Personalausweisnummer, German personal identity card number).
*
* The identity card of the Federal Republic of Germany is an official document as proof of
* identity for German nationals. In principle, the identity card authority responsible for
* the main residence assigns it at the request of the citizen.
*
* Source
* https://de.wikipedia.org/wiki/Personalausweis_(Deutschland)
* http://www.pruefziffernberechnung.de/P/Personalausweis-DE.shtml
* https://learn.microsoft.com/en-us/microsoft-365/compliance/sit-defn-germany-identity-card-number?view=o365-worldwide
* http://www.pruefziffernberechnung.de/Begleitdokumente/BKZ.shtml
*
* PERSON
*/
import * as exceptions from '../exceptions';
import { strings, isValidDateCompactYYMMDD, weightedSum } from '../util';
import { Validator, ValidateReturn } from '../types';
function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
const [value, err] = strings.cleanUnicode(input, ' -./,');
if (err) {
return [value, err];
}
// Old format IDs had the national code at position 11 new format has them at 24
if (/[A-Z]/.test(value[10])) {
const [p1, p2, p3, p4] = strings.splitAt(value, 10, 11, 25);
return [p1 + p3 + p2 + p4, err];
}
return [value, err];
}
const impl: Validator = {
name: 'German Personalausweisnummer',
localName: 'Personalausweisnummer',
abbreviation: 'PwNr',
compact(input: string): string {
const [value, err] = clean(input);
if (err) {
throw err;
}
return value;
},
format(input: string): string {
const [value] = clean(input);
return strings.splitAt(value, 10, 17, 24, 25).join(' ');
},
validate(input: string): ValidateReturn {
const [value, error] = clean(input);
if (error) {
return { isValid: false, error };
}
if (value.length !== 26) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!/^[CFGHJKLMNPRTVWXYZ][0-9CFGHJKLMNPRTVWXYZ]+[0-9]$/) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
const [
issue,
issueCheck,
birth,
birthCheck,
expiry,
expiryCheck,
nationality,
checksum,
] = strings.splitAt(value, 9, 10, 16, 17, 23, 24, 25);
console.log('GOT', value, issue, birth, expiry);
if (!isValidDateCompactYYMMDD(birth)) {
return {
isValid: false,
error: new exceptions.InvalidComponent('birthdate'),
};
}
if (!isValidDateCompactYYMMDD(expiry)) {
return {
isValid: false,
error: new exceptions.InvalidComponent('expiry'),
};
}
if (!/^[A-Z]$/.test(nationality)) {
return {
isValid: false,
error: new exceptions.InvalidComponent('nationality'),
};
}
const issueSum = weightedSum(issue, {
weights: [7, 3, 1],
modulus: 10,
});
const birthSum = weightedSum(birth, {
weights: [7, 3, 1],
modulus: 10,
});
const expirySum = weightedSum(expiry, {
weights: [7, 3, 1],
modulus: 10,
});
console.log('ISSUE', issue, issueSum, issueCheck);
if (String(issueSum) !== issueCheck) {
return {
isValid: false,
error: new exceptions.InvalidChecksum('issue.checksum'),
};
}
if (String(birthSum) !== birthCheck) {
return {
isValid: false,
error: new exceptions.InvalidChecksum('birth.checksum'),
};
}
if (String(expirySum) !== expiryCheck) {
return {
isValid: false,
error: new exceptions.InvalidChecksum('expiry.checksum'),
};
}
const sum = weightedSum(value.substring(0, 24), {
weights: [7, 3, 1],
modulus: 10,
});
if (String(sum) !== checksum) {
return {
isValid: false,
error: new exceptions.InvalidChecksum('checksum'),
};
}
return {
isValid: true,
compact: value,
isIndividual: true,
isCompany: false,
};
},
};
export const { name, localName, abbreviation, validate, format, compact } =
impl;