6
6
interface
7
7
8
8
uses
9
- Classes, SysUtils, bcrypt, DOM, xmlutils, XmlRead, XMLWrite, dialogs, windows, graphics;
9
+ Classes, SysUtils, bcrypt, DOM, xmlutils, XmlRead, XMLWrite, dialogs, windows,
10
+ graphics, math;
10
11
11
12
function canSignTables : boolean;
12
13
procedure signTable (cheattable: TDOMElement);
@@ -395,7 +396,7 @@ procedure signTableFile(f: string);
395
396
WriteXMLFile(d,f);
396
397
end ;
397
398
398
- procedure getPasswordHash (password: string; var hashbuffer : pointer; var hashlength : integer);
399
+ procedure generateHash (password: pointer; passwordsize: integer; var hash : pointer; var hashsize : integer);
399
400
var
400
401
s: ntstatus;
401
402
hashAlgoritm: BCRYPT_ALG_HANDLE;
@@ -405,12 +406,11 @@ procedure getPasswordHash(password: string; var hashbuffer: pointer; var hashlen
405
406
406
407
size: ulong;
407
408
i,j: integer;
409
+ secondaryvalue: byte;
408
410
begin
409
- if password=' ' then
410
- begin
411
- hashbuffer:=nil ;
412
- exit;
413
- end ;
411
+ hash:=nil ;
412
+ hashsize:=0 ;
413
+ if passwordsize=0 then exit;
414
414
415
415
s:=BCryptOpenAlgorithmProvider(hashAlgoritm, ' SHA512' , nil , 0 );
416
416
if succeeded(s) then
@@ -425,37 +425,93 @@ procedure getPasswordHash(password: string; var hashbuffer: pointer; var hashlen
425
425
s:=BCryptCreateHash(hashAlgoritm, hHash, bHashObject, objectlength, nil , 0 , 0 );
426
426
if succeeded(s) then
427
427
begin
428
- s:=BCryptHashData(hHash, @ password[ 1 ], length(password) , 0 );
428
+ s:=BCryptHashData(hHash, password, passwordsize , 0 );
429
429
if succeeded(s) then
430
430
begin
431
- s:=BCryptGetProperty(hashAlgoritm, BCRYPT_HASH_LENGTH, @hashlength , sizeof(DWORD), size, 0 );
431
+ s:=BCryptGetProperty(hashAlgoritm, BCRYPT_HASH_LENGTH, @hashsize , sizeof(DWORD), size, 0 );
432
432
if succeeded(s) then
433
433
begin
434
- getmem(hashbuffer, hashlength);
435
- s:=BCryptFinishHash(hHash, hashbuffer, hashlength, 0 );
434
+ getmem(hash, hashsize);
435
+ s:=BCryptFinishHash(hHash, hash, hashsize, 0 );
436
+
436
437
end ;
437
438
end ;
438
439
BCryptDestroyHash(hashAlgoritm);
439
440
end ;
440
441
freemem(bHashObject);
441
442
BCryptCloseAlgorithmProvider(hashAlgoritm,0 );
442
443
end ;
443
-
444
444
end ;
445
445
end ;
446
446
447
- procedure hashDecode (buffer: pbyte; buffersize: integer; hash: pbyte; hashsize: integer);
448
- var i,j: integer;
447
+ procedure getPasswordHash (password: string; out pwhash: pointer; out pwhashlength: integer; wantedsize: integer);
448
+ var
449
+ hash: array of byte;
450
+
451
+ initialHash: pbyte;
452
+ initialhashsize: integer;
453
+
454
+ partialhash: pbyte;
455
+ partialhashsize: integer;
456
+
457
+ hashpos: integer;
458
+ copysize: integer;
459
+ i,j: integer;
449
460
begin
450
- j:=0 ;
451
- for i:=0 to buffersize-1 do
461
+ pwhash:=nil ;
462
+ if password=' ' then exit;
463
+
464
+ setlength(hash,wantedsize);
465
+
466
+
467
+ // generate hashes until it's the size of the buffer
468
+ hashpos:=0 ;
469
+
470
+ generateHash(@password[1 ],length(password),initialhash,initialhashsize);
471
+
472
+ j:=1 ;
473
+ for i:=0 to initialhashsize-1 do
452
474
begin
453
- buffer [i]:=buffer [i] xor hash [j];
475
+ initialhash [i]:=initialhash [i] xor ord(password [j]) ;
454
476
inc(j);
455
- if j>=hashsize then j:=0 ;
477
+ if j>length(password) then j:=1 ;
456
478
end ;
479
+
480
+ generateHash(initialhash,initialhashsize,partialhash,partialhashsize);
481
+ freemem(initialhash);
482
+
483
+ copysize:=ifthen(partialhashsize>wantedsize, wantedsize, partialhashsize);
484
+ copymemory(@hash[0 ],partialhash, copysize);
485
+ freemem(partialhash);
486
+
487
+ inc(hashpos, copysize);
488
+
489
+ while hashpos<wantedsize do
490
+ begin
491
+ generateHash(@hash[hashpos-copysize],copysize,partialhash, partialhashsize);
492
+
493
+ copysize:=ifthen(partialhashsize+hashpos>wantedsize, wantedsize-hashpos, partialhashsize);
494
+ copymemory(@hash[hashpos],partialhash, copysize);
495
+ freemem(partialhash);
496
+ inc(hashpos,copysize);
497
+ end ;
498
+
499
+ getmem(pwhash, wantedsize);
500
+ copymemory(pwhash, @hash[0 ],wantedsize);
501
+
502
+ setlength(hash,0 );
503
+ end ;
504
+
505
+
506
+ procedure passwordDecode (buffer: pbyte; buffersize: integer; pwhash: pbyte);
507
+ var
508
+ i: integer;
509
+ begin
510
+ for i:=0 to buffersize-1 do
511
+ buffer[i]:=buffer[i] xor pwhash[i];
457
512
end ;
458
513
514
+
459
515
procedure signTable (cheattable: TDOMElement);
460
516
var
461
517
f: tfilestream;
@@ -546,7 +602,7 @@ procedure signTable(cheattable: TDOMElement);
546
602
end ;
547
603
548
604
if InputQuery(' CE Signature' , ' Enter your password' , true, password)=false then exit;
549
- getPasswordHash(password, pwhash, pwhashlength);
605
+ getPasswordHash(password, pwhash, pwhashlength, m.size-m.position );
550
606
end
551
607
else
552
608
begin
@@ -556,7 +612,7 @@ procedure signTable(cheattable: TDOMElement);
556
612
passwordhash:=nil ; // in case it's wrong/changed
557
613
558
614
if pwhash<>nil then
559
- HashDecode (pointer(ptruint(m.memory)+m.position), m.size-m.position, pwhash, pwhashlength );
615
+ passwordDecode (pointer(ptruint(m.memory)+m.position), m.size-m.position, pwhash);
560
616
end ;
561
617
562
618
try
0 commit comments