Subversion Repositories oidplus

Rev

Rev 1036 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1036 Rev 1376
Line 2... Line 2...
2
  'use strict';
2
  'use strict';
3
 
3
 
4
/**
4
/**
5
* WEID<=>OID Converter
5
* WEID<=>OID Converter
6
* (c) Webfan.de, ViaThinkSoft
6
* (c) Webfan.de, ViaThinkSoft
7
* Revision 2022-03-26
7
* Revision 2023-08-09
8
**/
8
**/
9
 
9
 
10
// What is a WEID?
10
// What is a WEID?
11
//     A WEID (WEhowski IDentifier) is an alternative representation of an
11
//     A WEID (WEhowski IDentifier) is an alternative representation of an
12
//     OID (Object IDentifier) defined by Till Wehowski.
12
//     OID (Object IDentifier) defined by Till Wehowski.
13
//     In OIDs, arcs are in decimal base 10. In WEIDs, the arcs are in base 36.
13
//     In OIDs, arcs are in decimal base 10. In WEIDs, the arcs are in base 36.
14
//     Also, each WEID has a check digit at the end (called WeLohn Check Digit).
14
//     Also, each WEID has a check digit at the end (called WeLuhn Check Digit).
15
//
15
//
16
// Changes in the December 2021 definition by Daniel Marschall:
16
// The full specification can be found here: https://weid.info/spec.html
-
 
17
//
-
 
18
// This converter supports WEID as of Spec Change #11
-
 
19
//
-
 
20
// A few short notes:
17
//     - There are several classes of WEIDs which have different OID bases:
21
//     - There are several classes of WEIDs which have different OID bases:
18
//           "Class C" WEID:  weid:EXAMPLE-3      (base .1.3.6.1.4.1.37553.8.)
-
 
19
//                            oid:1.3.6.1.4.1.37553.8.32488192274
-
 
20
//           "Class B" WEID:  weid:pen:SX0-7PR-6  (base .1.3.6.1.4.1.)
-
 
21
//                            oid:1.3.6.1.4.1.37476.9999
-
 
22
//           "Class A" WEID:  weid:root:2-RR-2    (base .)
22
//           "Class A" WEID:  weid:root:2-RR-?
23
//                            oid:2.999
23
//                            oid:2.999
-
 
24
//                            WEID class base OID: (OID Root)
-
 
25
//           "Class B" WEID:  weid:pen:SX0-7PR-?
-
 
26
//                            oid:1.3.6.1.4.1.37476.9999
-
 
27
//                            WEID class base OID: 1.3.6.1.4.1
-
 
28
//           "Class C" WEID:  weid:EXAMPLE-?
-
 
29
//                            oid:1.3.6.1.4.1.37553.8.32488192274
-
 
30
//                            WEID class base OID: 1.3.6.1.4.1.37553.8
-
 
31
//           "Class D" WEID:  weid:example.com:TEST-? is equal to weid:9-DNS-COM-EXAMPLE-TEST-?
-
 
32
//                            Since the check digit is based on the OID, the check digit is equal for both notations.
-
 
33
//                            oid:1.3.6.1.4.1.37553.8.9.17704.32488192274.16438.1372205
-
 
34
//                            WEID class base OID: 1.3.6.1.4.1.37553.8.9.17704
-
 
35
//     - The last arc in a WEID is the check digit. A question mark is the wildcard for an unknown check digit.
-
 
36
//       In this case, the converter will return the correct expected check digit for the input.
24
//     - The namespace (weid:, weid:pen:, weid:root:) is now case insensitive.
37
//     - The namespace (weid:, weid:pen:, weid:root:) is case insensitive.
25
//     - Padding with '0' characters is valid (e.g. weid:000EXAMPLE-3)
38
//     - Padding with '0' characters is valid (e.g. weid:000EXAMPLE-3)
26
//       The paddings do not count into the WeLuhn check-digit.
39
//       The paddings do not count into the WeLuhn check digit.
-
 
40
//
27
 
41
 
28
var WeidOidConverter = {
42
var WeidOidConverter = {
29
 
43
 
30
        weLuhnCheckDigit: function(str) {
44
        weLuhnCheckDigit: function(str) {
31
                // Padding zeros don't count to the check digit (December 2021)
45
                // Padding zeros don't count to the check digit (December 2021)
Line 97... Line 111...
97
                return oid;
111
                return oid;
98
        },
112
        },
99
 
113
 
100
        // Translates a WEID to an OID
114
        // Translates a WEID to an OID
101
        // "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274"
115
        // "weid:EXAMPLE-3" becomes "1.3.6.1.4.1.37553.8.32488192274"
102
        // If it failed (e.g. wrong namespace, wrong checksum, etc.) then false is returned.
116
        // If it failed (e.g. wrong namespace, wrong check digit, etc.) then false is returned.
103
        // If the weid ends with '?', the checksum will be added
117
        // If the weid ends with '?', the check digit will be added
104
        // Return value is an array with the elements "oid" and "weid".
118
        // Return value is an array with the elements "oid" and "weid".
105
        // Example:
119
        // Example:
106
        //     weid2oid("weid:EXAMPLE-?").weid == "weid:EXAMPLE-3"
120
        //     weid2oid("weid:EXAMPLE-?").weid == "weid:EXAMPLE-3"
107
        //     weid2oid("weid:EXAMPLE-?").oid  == "1.3.6.1.4.1.37553.8.32488192274"
121
        //     weid2oid("weid:EXAMPLE-?").oid  == "1.3.6.1.4.1.37553.8.32488192274"
108
        weid2oid: function(weid) {
122
        weid2oid: function(weid) {
Line 112... Line 126...
112
                var namespace = weid.substr(0, p+1);
126
                var namespace = weid.substr(0, p+1);
113
                var rest = weid.substr(p+1);
127
                var rest = weid.substr(p+1);
114
 
128
 
115
                var base = null;
129
                var base = null;
116
                namespace = namespace.toLowerCase(); // namespace is case insensitive
130
                namespace = namespace.toLowerCase(); // namespace is case insensitive
-
 
131
 
-
 
132
                if (namespace.startsWith("weid:")) {
-
 
133
                        var domainpart = weid.split(":")[1].split(".");
-
 
134
                        if (domainpart.length > 1) {
-
 
135
                                // Spec Change 10: Class D / Domain-WEID ( https://github.com/frdl/weid/issues/3 )
-
 
136
                                if (weid.split(":").length != 3) return false;
-
 
137
                                var domainrest = weid.split(":")[2].split("-");
-
 
138
                                var alt_weid = "weid:9-DNS-" + domainpart.reverse().join("-").toUpperCase() + "-" + domainrest.join("-");
-
 
139
                                return WeidOidConverter.weid2oid(alt_weid);
-
 
140
                        }
-
 
141
                }
-
 
142
 
-
 
143
                if (namespace.startsWith('weid:x-')) {
-
 
144
                        // Spec Change 11: Proprietary Namespaces ( https://github.com/frdl/weid/issues/4 )
-
 
145
                        return { "weid": weid, "oid" : "[Proprietary WEID Namespace]" };
117
                if (namespace == 'weid:') {
146
                } else if (namespace == 'weid:') {
118
                        // Class C
147
                        // Class C
119
                        base = '1-3-6-1-4-1-SZ5-8';
148
                        base = '1-3-6-1-4-1-SZ5-8';
120
                } else if (namespace == 'weid:pen:') {
149
                } else if (namespace == 'weid:pen:') {
121
                        // Class B
150
                        // Class B
122
                        base = '1-3-6-1-4-1';
151
                        base = '1-3-6-1-4-1';
Line 141... Line 170...
141
 
170
 
142
                var actual_checksum = elements.pop();
171
                var actual_checksum = elements.pop();
143
                var expected_checksum = WeidOidConverter.weLuhnCheckDigit(elements.join('-'));
172
                var expected_checksum = WeidOidConverter.weLuhnCheckDigit(elements.join('-'));
144
                if (actual_checksum != '?') {
173
                if (actual_checksum != '?') {
145
                        if (actual_checksum != expected_checksum) {
174
                        if (actual_checksum != expected_checksum) {
146
                                console.error("weid2oid: Wrong checksum");
175
                                console.error("weid2oid: Wrong check digit");
147
                                return false; // wrong checksum
176
                                return false; // wrong checksum
148
                        }
177
                        }
149
                } else {
178
                } else {
150
                        // If checksum is '?', it will be replaced by the actual checksum,
179
                        // If check digit is '?', it will be replaced by the actual check digit,
151
                        // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
180
                        // e.g. weid:EXAMPLE-? becomes weid:EXAMPLE-3
152
                        weid = weid.replace('?', expected_checksum);
181
                        weid = weid.replace('?', expected_checksum);
153
                }
182
                }
154
                elements.forEach(function (o,i,a) {
183
                elements.forEach(function (o,i,a) {
155
                        a[i] = WeidOidConverter.base_convert_bigint(a[i], 36, 10);
184
                        a[i] = WeidOidConverter.base_convert_bigint(a[i], 36, 10);