forked from larshp/abapNTLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zcl_md4.clas.abap
507 lines (397 loc) · 11.8 KB
/
zcl_md4.clas.abap
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
class ZCL_MD4 definition
public
create public .
public section.
*"* public components of class ZCL_MD4
*"* do not include other source files here!!!
types:
TY_BYTE4 type x length 4 .
types:
TY_BYTE16 type x length 16 .
type-pools ABAP .
class ZCL_MD4 definition load .
class-methods HASH
importing
!IV_STRING type CLIKE
!IV_ENCODING type ABAP_ENCODING default 'UTF-8'
returning
value(RV_HASH) type ZCL_MD4=>TY_BYTE16 .
class-methods HASH_HEX
importing
!IV_XSTR type XSTRING
returning
value(RV_HASH) type ZCL_MD4=>TY_BYTE16 .
protected section.
*"* protected components of class ZCL_MD4
*"* do not include other source files here!!!
class-methods OVERFLOW
importing
!IV_F type F
returning
value(RV_RES) type TY_BYTE4 .
class-methods SHIFT
importing
!IV_INPUT type TY_BYTE4
!IV_PLACES type I
returning
value(RV_OUTPUT) type TY_BYTE4 .
class-methods X
importing
!IV_XSTR type XSTRING
!IV_BLOCK type I
!IV_WORD type I
returning
value(RV_WORD) type TY_BYTE4 .
class-methods FUNC_F
importing
!IV_X type TY_BYTE4
!IV_Y type TY_BYTE4
!IV_Z type TY_BYTE4
returning
value(RV_RES) type TY_BYTE4 .
class-methods FUNC_FF
importing
!IV_A type TY_BYTE4
!IV_B type TY_BYTE4
!IV_C type TY_BYTE4
!IV_D type TY_BYTE4
!IV_X type TY_BYTE4
!IV_S type I
returning
value(RV_RES) type TY_BYTE4 .
class-methods FUNC_GG
importing
!IV_A type TY_BYTE4
!IV_B type TY_BYTE4
!IV_C type TY_BYTE4
!IV_D type TY_BYTE4
!IV_X type TY_BYTE4
!IV_S type I
returning
value(RV_RES) type TY_BYTE4 .
class-methods FUNC_HH
importing
!IV_A type TY_BYTE4
!IV_B type TY_BYTE4
!IV_C type TY_BYTE4
!IV_D type TY_BYTE4
!IV_X type TY_BYTE4
!IV_S type I
returning
value(RV_RES) type TY_BYTE4 .
class-methods FUNC_G
importing
!IV_X type TY_BYTE4
!IV_Y type TY_BYTE4
!IV_Z type TY_BYTE4
returning
value(RV_RES) type TY_BYTE4 .
class-methods FUNC_H
importing
!IV_X type TY_BYTE4
!IV_Y type TY_BYTE4
!IV_Z type TY_BYTE4
returning
value(RV_RES) type TY_BYTE4 .
class-methods PADDING_LENGTH
importing
!IV_INPUT type XSTRING
returning
value(RV_XSTRING) type XSTRING .
type-pools ABAP .
class-methods CODEPAGE
importing
!IV_ENCODING type ABAP_ENCODING default 'UTF-8'
!IV_STRING type CLIKE
returning
value(RV_XSTRING) type XSTRING .
private section.
*"* private components of class ZCL_MD4
*"* do not include other source files here!!!
ENDCLASS.
CLASS ZCL_MD4 IMPLEMENTATION.
METHOD codepage.
DATA: lo_obj TYPE REF TO cl_abap_conv_out_ce.
lo_obj = cl_abap_conv_out_ce=>create( encoding = iv_encoding ).
lo_obj->convert( EXPORTING data = iv_string
IMPORTING buffer = rv_xstring ).
ENDMETHOD.
METHOD func_f.
* XY v not(X) Z
rv_res = ( iv_x BIT-AND iv_y ) BIT-OR ( ( BIT-NOT iv_x ) BIT-AND iv_z ).
ENDMETHOD.
METHOD func_ff.
DATA: lv_f TYPE f.
* (a + F(b,c,d) + X[k]) <<< s
lv_f = iv_a +
func_f( iv_x = iv_b
iv_y = iv_c
iv_z = iv_d ) +
iv_x.
rv_res = overflow( lv_f ).
rv_res = shift( iv_input = rv_res
iv_places = iv_s ).
ENDMETHOD.
METHOD func_g.
* XY v XZ v YZ
rv_res = ( iv_x BIT-AND iv_y )
BIT-OR ( iv_x BIT-AND iv_z )
BIT-OR ( iv_y BIT-AND iv_z ).
ENDMETHOD.
METHOD func_gg.
CONSTANTS: lc_add TYPE x LENGTH 4 VALUE '5A827999'.
DATA: lv_f TYPE f.
* (a + G(b,c,d) + X[k] + 5A827999) <<< s
lv_f = iv_a +
func_g( iv_x = iv_b
iv_y = iv_c
iv_z = iv_d ) +
iv_x + lc_add.
rv_res = overflow( lv_f ).
rv_res = shift( iv_input = rv_res
iv_places = iv_s ).
ENDMETHOD.
METHOD func_h.
* X xor Y xor Z
rv_res = ( iv_x BIT-XOR iv_y ) BIT-XOR iv_z.
ENDMETHOD.
METHOD func_hh.
CONSTANTS: lc_add TYPE x LENGTH 4 VALUE '6ED9EBA1'.
DATA: lv_f TYPE f.
* (a + H(b,c,d) + X[k] + 6ED9EBA1) <<< s
lv_f = iv_a +
func_h( iv_x = iv_b
iv_y = iv_c
iv_z = iv_d ) +
iv_x + lc_add.
rv_res = overflow( lv_f ).
rv_res = shift( iv_input = rv_res
iv_places = iv_s ).
ENDMETHOD.
METHOD hash.
* The MIT License (MIT)
*
* Copyright (c) 2015 Lars Hvam
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* http://tools.ietf.org/html/rfc1320
DATA: lv_xstr TYPE xstring.
lv_xstr = codepage( iv_encoding = iv_encoding
iv_string = iv_string ).
rv_hash = hash_hex( lv_xstr ).
ENDMETHOD.
METHOD hash_hex.
* The MIT License (MIT)
*
* Copyright (c) 2015 Lars Hvam
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
DATA: lv_xstr TYPE xstring.
* big endian
DATA: lv_a TYPE x LENGTH 4 VALUE '67452301',
lv_b TYPE x LENGTH 4 VALUE 'EFCDAB89',
lv_c TYPE x LENGTH 4 VALUE '98BADCFE',
lv_d TYPE x LENGTH 4 VALUE '10325476'.
DATA: lv_aa TYPE x LENGTH 4,
lv_bb TYPE x LENGTH 4,
lv_cc TYPE x LENGTH 4,
lv_dd TYPE x LENGTH 4.
DATA: lv_x TYPE ty_byte4,
lv_f TYPE f,
lv_block TYPE i.
DEFINE _ff.
lv_x = x(
iv_xstr = lv_xstr
iv_block = lv_block
iv_word = &5 ).
&1 = func_ff(
iv_a = &1
iv_b = &2
iv_c = &3
iv_d = &4
iv_x = lv_x
iv_s = &6 ).
END-OF-DEFINITION.
DEFINE _gg.
lv_x = x(
iv_xstr = lv_xstr
iv_block = lv_block
iv_word = &5 ).
&1 = func_gg(
iv_a = &1
iv_b = &2
iv_c = &3
iv_d = &4
iv_x = lv_x
iv_s = &6 ).
END-OF-DEFINITION.
DEFINE _hh.
lv_x = x(
iv_xstr = lv_xstr
iv_block = lv_block
iv_word = &5 ).
&1 = func_hh(
iv_a = &1
iv_b = &2
iv_c = &3
iv_d = &4
iv_x = lv_x
iv_s = &6 ).
END-OF-DEFINITION.
lv_xstr = padding_length( iv_xstr ).
* 16 words = 64 byte
DO xstrlen( lv_xstr ) / 64 TIMES.
lv_block = sy-index.
lv_aa = lv_a.
lv_bb = lv_b.
lv_cc = lv_c.
lv_dd = lv_d.
* round 1
_ff lv_a lv_b lv_c lv_d 0 3.
_ff lv_d lv_a lv_b lv_c 1 7.
_ff lv_c lv_d lv_a lv_b 2 11.
_ff lv_b lv_c lv_d lv_a 3 19.
_ff lv_a lv_b lv_c lv_d 4 3.
_ff lv_d lv_a lv_b lv_c 5 7.
_ff lv_c lv_d lv_a lv_b 6 11.
_ff lv_b lv_c lv_d lv_a 7 19.
_ff lv_a lv_b lv_c lv_d 8 3.
_ff lv_d lv_a lv_b lv_c 9 7.
_ff lv_c lv_d lv_a lv_b 10 11.
_ff lv_b lv_c lv_d lv_a 11 19.
_ff lv_a lv_b lv_c lv_d 12 3.
_ff lv_d lv_a lv_b lv_c 13 7.
_ff lv_c lv_d lv_a lv_b 14 11.
_ff lv_b lv_c lv_d lv_a 15 19.
* round 2
_gg lv_a lv_b lv_c lv_d 0 3.
_gg lv_d lv_a lv_b lv_c 4 5.
_gg lv_c lv_d lv_a lv_b 8 9.
_gg lv_b lv_c lv_d lv_a 12 13.
_gg lv_a lv_b lv_c lv_d 1 3.
_gg lv_d lv_a lv_b lv_c 5 5.
_gg lv_c lv_d lv_a lv_b 9 9.
_gg lv_b lv_c lv_d lv_a 13 13.
_gg lv_a lv_b lv_c lv_d 2 3.
_gg lv_d lv_a lv_b lv_c 6 5.
_gg lv_c lv_d lv_a lv_b 10 9.
_gg lv_b lv_c lv_d lv_a 14 13.
_gg lv_a lv_b lv_c lv_d 3 3.
_gg lv_d lv_a lv_b lv_c 7 5.
_gg lv_c lv_d lv_a lv_b 11 9.
_gg lv_b lv_c lv_d lv_a 15 13.
* round 3
_hh lv_a lv_b lv_c lv_d 0 3.
_hh lv_d lv_a lv_b lv_c 8 9.
_hh lv_c lv_d lv_a lv_b 4 11.
_hh lv_b lv_c lv_d lv_a 12 15.
_hh lv_a lv_b lv_c lv_d 2 3.
_hh lv_d lv_a lv_b lv_c 10 9.
_hh lv_c lv_d lv_a lv_b 6 11.
_hh lv_b lv_c lv_d lv_a 14 15.
_hh lv_a lv_b lv_c lv_d 1 3.
_hh lv_d lv_a lv_b lv_c 9 9.
_hh lv_c lv_d lv_a lv_b 5 11.
_hh lv_b lv_c lv_d lv_a 13 15.
_hh lv_a lv_b lv_c lv_d 3 3.
_hh lv_d lv_a lv_b lv_c 11 9.
_hh lv_c lv_d lv_a lv_b 7 11.
_hh lv_b lv_c lv_d lv_a 15 15.
lv_f = lv_a + lv_aa.
lv_a = overflow( lv_f ).
lv_f = lv_b + lv_bb.
lv_b = overflow( lv_f ).
lv_f = lv_c + lv_cc.
lv_c = overflow( lv_f ).
lv_f = lv_d + lv_dd.
lv_d = overflow( lv_f ).
ENDDO.
CONCATENATE
lv_a+3(1) lv_a+2(1) lv_a+1(1) lv_a(1)
lv_b+3(1) lv_b+2(1) lv_b+1(1) lv_b(1)
lv_c+3(1) lv_c+2(1) lv_c+1(1) lv_c(1)
lv_d+3(1) lv_d+2(1) lv_d+1(1) lv_d(1)
INTO rv_hash IN BYTE MODE.
ENDMETHOD.
METHOD overflow.
DATA: lv_f TYPE f,
lv_maxint TYPE i.
lv_maxint = 2 ** 31 - 1.
lv_f = iv_f.
IF iv_f < - lv_maxint OR iv_f > lv_maxint.
lv_f = ( iv_f + ( lv_maxint + 1 ) ) MOD ( 2 * ( lv_maxint + 1 ) ) - lv_maxint - 1.
ENDIF.
rv_res = lv_f.
ENDMETHOD.
METHOD padding_length.
CONSTANTS: lc_x0 TYPE x LENGTH 1 VALUE '00',
lc_x1 TYPE x LENGTH 1 VALUE '80'.
DATA: lv_length TYPE x LENGTH 8. " double word
CONCATENATE iv_input lc_x1 INTO rv_xstring IN BYTE MODE.
WHILE xstrlen( rv_xstring ) MOD 64 <> 56.
CONCATENATE rv_xstring lc_x0 INTO rv_xstring IN BYTE MODE.
ENDWHILE.
lv_length = xstrlen( iv_input ) * 8. " get number of bits
CONCATENATE rv_xstring
lv_length+7(1) lv_length+6(1) lv_length+5(1) lv_length+4(1)
lv_length+3(1) lv_length+2(1) lv_length+1(1) lv_length(1)
INTO rv_xstring IN BYTE MODE.
ENDMETHOD.
METHOD shift.
DATA: lv_bits TYPE c LENGTH 32,
lv_offset TYPE i,
lv_bit TYPE c LENGTH 1.
DO 32 TIMES.
GET BIT sy-index OF iv_input INTO lv_bit.
CONCATENATE lv_bits lv_bit INTO lv_bits.
ENDDO.
SHIFT lv_bits LEFT CIRCULAR BY iv_places PLACES.
DO 32 TIMES.
lv_offset = sy-index - 1.
lv_bit = lv_bits+lv_offset(1).
SET BIT lv_offset + 1 OF rv_output TO lv_bit.
ENDDO.
ENDMETHOD.
METHOD x.
DATA: lv_offset TYPE i,
lv_x TYPE x.
lv_offset = ( ( iv_block - 1 ) * 16 + iv_word ) * 4.
DO 4 TIMES.
lv_x = iv_xstr+lv_offset(1).
CONCATENATE lv_x rv_word INTO rv_word IN BYTE MODE.
lv_offset = lv_offset + 1.
ENDDO.
ENDMETHOD.
ENDCLASS.