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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
| using System;
//#####################################################################################################################
//https://metadataconsulting.blogspot.com/2019/12/CSharp-Convert-a-GUID-to-a-Darwin-Descriptor-and-back.html
//#####################################################################################################################
public static class Program
{
class GUID2DD
{
// Conversion of this VB code to C# by http://metadataconsulting.blogspot.com/
// Some portions modernized, but an interesting exercise converting from a old language unexpressive to more modern lang
// Source Code: https://www.symantec.com/connect/articles/working-darwin-descriptors
//
// Auto VB translators
// 1.https://www.developerfusion.com/tools/convert/vb-to-csharp/?batchId=8e249124-06d2-4955-a238-dc2914a9cd3d - not good
// 2.https://www.carlosag.net/tools/codetranslator/ - super fast!
// 'VB Code - Copyright EdT 2012
// 'Script converts standard GUID to Darwin Descriptor and vice versa
// 'A standard GUID requires 38 characters and contains 5 groups of hexadecimal characters separated by dashes.
// 'The valid format for a standard GUID is:
// '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
// 'where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
//
// 'This program can copy the result of the conversion process to the clipboard.
// 'To do this it uses the MS Office Word automation object, so for this to work,
// 'a version of MSOffice must be present. Unfortunately, VBScript does not have
// 'a native method for copying variables to the clipboard.
public string GetConvertedGUID(string strProductCode) {
string Block1;
string Block2;
string Block3;
string Block4;
string strMungedCode = string.Empty;
long Dec1;
long Dec2;
long Dec3;
long Dec4;
string strPart1;
string strPart2;
string strPart3;
string strPart4;
const int intArraySize = 32;
int[] arrSequence = new int[]{2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 11, 12, 13, 14, 28, 29, 26, 27, 23, 24, 21, 22, 36, 37, 34, 35, 32, 33, 30, 31};
// Generate the RegProduct Code
for (int intIndex = 0; intIndex <= (intArraySize - 1); intIndex++)
{
strMungedCode = (strMungedCode + strProductCode.Substring((arrSequence[intIndex] - 1), 1));
}
Block1 = strMungedCode.Substring(0, 8);
Block2 = strMungedCode.Substring(8, 8);
Block3 = strMungedCode.Substring(16, 8);
Block4 = strMungedCode.Substring(24, 8);
Dec1 = HextoDec(Block1);
strPart1 = DecToBase85(Dec1);
Dec2 = HextoDec(Block2);
strPart2 = DecToBase85(Dec2);
Dec3 = HextoDec(Block3);
strPart3 = DecToBase85(Dec3);
Dec4 = HextoDec(Block4);
strPart4 = DecToBase85(Dec4);
return (strPart1.ReverseString()
+ (strPart2
+ (strPart3 + strPart4.ReverseString())));
}
public long HextoDec(string strHex)
{
//string strDigit;
//int intDigit;
//int intValue;
long lngResult = 0;
////for (int intIndex = strHex.Length; (intIndex <= 1); intIndex = (intIndex + -1))
//for (int intIndex = strHex.Length; intIndex >= 1; intIndex--)
//{
// strDigit = strHex.Substring((intIndex - 1), 1);
// intDigit = (("0123456789ABCDEF".IndexOf(strDigit.ToUpper()) + 1) - 1);
// if ((intDigit >= 0)) {
// //intValue = (intDigit * (16 | (strHex.Length - intIndex)));
// // TODO: Warning!!! The operator should be an XOR ^ instead of an OR, but not available in CodeDOM
// //Mon 09-Dec-19 6:54pm metadataconsulting.ca - XOR used
// intValue = (intDigit * (16 ^ (strHex.Length - intIndex)));
// lngResult = (lngResult + intValue);
// }
// else {
// lngResult = 0;
// intIndex = 0;
// //stop the loop
// }
//}
lngResult = Convert.ToInt64(strHex, 16);
return lngResult;
}
private string DecitoHex(int ddec)
{
//// Must return eight hex bytes
//decimal hex1;
//string hex2 = "";
//int hexlen;
//while ((strDec > 15)) {
// hex1 = (( (strDec / 16) - (int)(strDec / 16) ) * 16);
// hex2 = ("0123456789ABCDEF".Substring(hex1.ToString(), 1) + hex2);
// strDec = int((strDec / 16));
// hex2 = ("0123456789ABCDEF".Substring(strDec, 1) + hex2);
// hexlen = hex2.Length;
// DecToHex = (string((8 - hexlen), "0") + hex2);
//}
return ddec.ToString("X8");
}
public string DecToBase85(long strDecimal) {
// Multiples of 85 = 85, 7225, 614125, 52200625
int strDec = (int)strDecimal;
string Base85 = "!$%&\'()*+,-.0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
int num;
string quint1;
string quint2;
string quint3;
string quint4;
string quint5;
//int Offset;
quint1 = "!";
quint2 = "!";
quint3 = "!";
quint4 = "!";
quint5 = "!";
if ((strDecimal < 85))
{
//quint1 = Base85.Substring(strDec, 1);
long mod = strDecimal % 85;
num = (int)mod;
quint1 = Base85.Substring(num, 1);
}
if (strDecimal > 84 && strDecimal < 7225)
{
// Offset = strDec;
// strDec = (int)((strDec / 85));
// num = (((Offset / 85)
// - strDec)
// * 85);
// quint1 = Base85.Substring(num, 1);
// quint2 = Base85.Substring(strDec, 1);
num = (int)(strDecimal / 85) % 85;
quint1 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
num = (int)mod;
quint2 = Base85.Substring(num, 1);
}
if (((strDecimal > 7224) && (strDecimal < 614125)))
{
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
//quint1 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
//quint2 = Base85.Substring(num, 1);
//quint3 = Base85.Substring(strDec, 1);
num = (int)(strDecimal / 7225) % 85;
quint1 = Base85.Substring(num, 1);
num = (int)(strDecimal / 85) % 85;
quint2 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
num = (int)mod;
quint3 = Base85.Substring(num, 1);
}
if (((strDecimal > 614124) && (strDecimal < 52200625)))
{
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
//quint1 = Base85.Substring(num, 1);
num = (int)(strDecimal / 614125) % 85;
quint1 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
//quint2 = Base85.Substring(num, 1);
num = (int)(strDecimal / 7225) % 85;
quint2 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
//quint3 = Base85.Substring(num, 1);
//quint4 = Base85.Substring(strDec, 1);
num = (int)(strDecimal / 85) % 85;
quint3 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
num = (int)mod;
quint4 = Base85.Substring(num, 1);
}
if ((strDecimal > 52200624))
{
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
num = (int)(strDecimal/52200625) % 85;
quint1 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
num = (int)(strDecimal / 614125) % 85;
quint2 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
num = (int)(strDecimal / 7225) % 85;
quint3 = Base85.Substring(num, 1);
//Offset = strDec;
//strDec = (int)((strDec / 85));
//num = (((Offset / 85)
// - strDec)
// * 85);
num = (int)(strDecimal / 85) % 85;
quint4 = Base85.Substring(num, 1);
long mod = strDecimal % 85;
num = (int)mod;
quint5 = Base85.Substring(num, 1);
}
return (quint1
+ (quint2
+ (quint3
+ (quint4 + quint5))));
}
//Function DDtoHex(darwin) renamed from VB
public string DDtoGUID(string darwin) {
string quad1;
string quad2;
string quad3;
string quad4;
string mungedStr;
quad1 = darwin.Substring(0, 5);
quad1 = QuadtoHex(quad1);
quad2 = darwin.Substring(5, 5);
quad2 = QuadtoHex(quad2);
quad3 = darwin.Substring(10, 5);
quad3 = QuadtoHex(quad3);
quad4 = darwin.Substring(15, 5);
quad4 = QuadtoHex(quad4);
mungedStr = (quad1
+ (quad2
+ (quad3 + quad4)));
return ReconstructProductCode(mungedStr);
}
public string QuadtoHex(string quad) {
char chr;
string Base85;
int b85;
int ddec;
Base85 = "!$%&\'()*+,-.0123456789=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~";
ddec = 0;
for (int i = 5; i >= 1; i--)
{
//chr = (char)quad.Substring((i - 1), 1);
chr = quad[(i-1)];
b85 = ((Base85.IndexOf(chr) + 1) - 1);
ddec = (ddec + b85);
if ((i > 1)) {
ddec = (ddec * 85);
}
}
return DecitoHex(ddec);
}
public string ReconstructProductCode(string munged) {
string strProductCode;
const int intArraySize = 32;
strProductCode = "{";
int[] arrSequence = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 13, 14, 15, 16, 9, 10, 11, 12, 23, 24, 21, 22, 19, 20, 17, 18, 31, 32, 29, 30, 27, 28, 25, 26};
// // Generate the Product Code
for (int intIndex = 0; (intIndex
<= (intArraySize - 1)); intIndex++) {
strProductCode = (strProductCode + munged.Substring((arrSequence[intIndex] - 1), 1));
if (((intIndex == 7)
|| ((intIndex == 11)
|| ((intIndex == 15)
|| (intIndex == 19))))) {
strProductCode = (strProductCode + "-");
}
}
return (strProductCode + "}");
}
}
public static string ReverseString(this string s)
{
if (string.IsNullOrEmpty(s))
return s;
char[] array = new char[s.Length];
int forward = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
array[forward++] = s[i];
}
return new string(array);
}
public static void Main()
{
GUID2DD conv = new GUID2DD();
string code = "w_1^VX!!!!!!!!!MKKSk";
//string code = "{91120000-0030-0000-0000-0000000ff1ce}"; //expect from above
string result = string.Empty;
// check if this is a GUID
if ((code.Length == 38))
{
if ((code.Substring(0, 1) == "{"))
result = conv.GetConvertedGUID(code);
}
else if ((code.Length == 20))
{
result = conv.DDtoGUID(code);
}
else
{
result = "Format of string submitted does not match that of a regular GUID or a Darwin Descriptor. Entries must" +
" be of the form {xxxxxxxx-xxxx-xxxx-xxxx-Xxxxxxxxxxxx} or xxxxxxxxxxxxxxxxxxxx (len=20)";
}
Console.WriteLine(code+" => " + result);
}
}
|