Cleanup
This commit is contained in:
parent
5ff385bf74
commit
82cd604c2f
190
mdl/m_crypt.pas
190
mdl/m_crypt.pas
|
@ -4,8 +4,10 @@ Unit m_Crypt;
|
|||
|
||||
Interface
|
||||
|
||||
// merge in m_Crc, rewrite googled and shitty hextobyte function
|
||||
// m_CRC should be merged into this
|
||||
|
||||
Function B64Encode (S: String) : String;
|
||||
Function B64Decode (S: String) : String;
|
||||
Function HMAC_MD5 (Text, Key: String) : String;
|
||||
Function MD5 (Const Value: String) : String;
|
||||
Function Digest2String (Digest: String) : String;
|
||||
|
@ -28,41 +30,70 @@ Type
|
|||
BufLong : array[0..15] of Integer;
|
||||
End;
|
||||
|
||||
// rewrite the terribad functions now that this stuff is working
|
||||
Const
|
||||
B64Codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
|
||||
function HexToByte (Hex: String) : byte;
|
||||
var
|
||||
n, n2, sayi: Integer;
|
||||
Function B64Encode (S: String) : String;
|
||||
Var
|
||||
I : Integer;
|
||||
A : Integer;
|
||||
X : Integer;
|
||||
B : Integer;
|
||||
Begin
|
||||
n := 0;
|
||||
n2 := 0;
|
||||
Result := '';
|
||||
A := 0;
|
||||
B := 0;
|
||||
|
||||
Case Hex[1] Of
|
||||
'A','a': n:= 10;
|
||||
'B','b': n:= 11;
|
||||
'C','c': n:= 12;
|
||||
'D','d': n:= 13;
|
||||
'E','e': n:= 14;
|
||||
'F','f': n:= 15;
|
||||
For I := 1 to Length(S) Do Begin
|
||||
X := Byte(S[I]);
|
||||
B := B * 256 + X;
|
||||
A := A + 8;
|
||||
|
||||
While A >= 6 Do Begin
|
||||
A := A - 6;
|
||||
X := B DIV (1 SHL a);
|
||||
B := B MOD (1 SHL a);
|
||||
Result := Result + B64Codes[X + 1];
|
||||
End;
|
||||
End;
|
||||
|
||||
If Hex[1] in ['0','1','2','3','4','5','6','7','8','9'] Then
|
||||
n := strS2I(Hex[1]);
|
||||
If A > 0 Then Begin
|
||||
X := B SHL (6 - A);
|
||||
Result := Result + B64Codes[X + 1];
|
||||
|
||||
Case Hex[2] Of
|
||||
'A','a': n2:= 10;
|
||||
'B','b': n2:= 11;
|
||||
'C','c': n2:= 12;
|
||||
'D','d': n2:= 13;
|
||||
'E','e': n2:= 14;
|
||||
'F','f': n2:= 15;
|
||||
If A = 4 Then Result := Result + '=';
|
||||
If A = 2 Then Result := Result + '==';
|
||||
End;
|
||||
End;
|
||||
|
||||
If Hex[2] in ['0','1','2','3','4','5','6','7','8','9'] Then
|
||||
n2 := strS2I(Hex[2]);
|
||||
Function B64Decode (S: String) : String;
|
||||
Var
|
||||
I : Integer;
|
||||
A : Integer;
|
||||
X : Integer;
|
||||
B : Integer;
|
||||
Begin
|
||||
Result := '';
|
||||
A := 0;
|
||||
B := 0;
|
||||
|
||||
sayi := n * 16 + n2;
|
||||
Result := sayi;
|
||||
For I := 1 to Length(S) Do Begin
|
||||
X := Pos(s[I], B64Codes) - 1;
|
||||
|
||||
If X >= 0 Then Begin
|
||||
B := B * 64 + X;
|
||||
A := A + 6;
|
||||
|
||||
If A >= 8 Then Begin
|
||||
A := A - 8;
|
||||
X := B SHR A;
|
||||
B := B MOD (1 SHL A);
|
||||
X := X MOD 256;
|
||||
Result := Result + Chr(X);
|
||||
End;
|
||||
End Else
|
||||
Exit;
|
||||
End;
|
||||
End;
|
||||
|
||||
Function Digest2String (Digest: String) : String;
|
||||
|
@ -85,7 +116,7 @@ Begin
|
|||
Count := 1;
|
||||
|
||||
While Count < Length(Str) Do Begin
|
||||
Result := Result + Char(HexToByte(Copy(Str, Count, 2)));
|
||||
Result := Result + Char(strH2I(Copy(Str, Count, 2)));
|
||||
Inc (Count, 2);
|
||||
End;
|
||||
End;
|
||||
|
@ -136,12 +167,12 @@ Begin
|
|||
|
||||
Inc(Count[0], InputLen shl 3);
|
||||
|
||||
if Count[0] < (InputLen shl 3) then
|
||||
If Count[0] < (InputLen shl 3) then
|
||||
Inc(Count[1]);
|
||||
|
||||
Inc(Count[1], InputLen shr 29);
|
||||
|
||||
partLen := 64 - Index;
|
||||
PartLen := 64 - Index;
|
||||
|
||||
if InputLen >= partLen then begin
|
||||
ArrLongToByte(BufLong, BufAnsiChar);
|
||||
|
@ -163,47 +194,48 @@ Begin
|
|||
end;
|
||||
|
||||
Index := 0;
|
||||
end else
|
||||
End Else
|
||||
I := 0;
|
||||
|
||||
ArrLongToByte(BufLong, BufAnsiChar);
|
||||
Move(Data[I+1], BufAnsiChar[Index], InputLen-I);
|
||||
ArrByteToLong(BufAnsiChar, BufLong);
|
||||
end
|
||||
end;
|
||||
End
|
||||
End;
|
||||
|
||||
procedure MD5Transform(var Buf: array of LongInt; const Data: array of LongInt);
|
||||
var
|
||||
Procedure MD5Transform (Var Buf: Array of LongInt; const Data: Array of LongInt);
|
||||
Var
|
||||
A, B, C, D: LongInt;
|
||||
|
||||
procedure Round1(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
begin
|
||||
Procedure Round1 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
Begin
|
||||
Inc(W, (Z xor (X and (Y xor Z))) + Data);
|
||||
W := (W shl S) or (W shr (32 - S));
|
||||
Inc(W, X);
|
||||
end;
|
||||
End;
|
||||
|
||||
procedure Round2(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
begin
|
||||
Procedure Round2 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
Begin
|
||||
Inc(W, (Y xor (Z and (X xor Y))) + Data);
|
||||
W := (W shl S) or (W shr (32 - S));
|
||||
Inc(W, X);
|
||||
end;
|
||||
End;
|
||||
|
||||
procedure Round3(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
begin
|
||||
Procedure Round3 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
Begin
|
||||
Inc(W, (X xor Y xor Z) + Data);
|
||||
W := (W shl S) or (W shr (32 - S));
|
||||
Inc(W, X);
|
||||
end;
|
||||
End;
|
||||
|
||||
procedure Round4(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
begin
|
||||
Procedure Round4 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||
Begin
|
||||
Inc(W, (Y xor (X or not Z)) + Data);
|
||||
W := (W shl S) or (W shr (32 - S));
|
||||
Inc(W, X);
|
||||
end;
|
||||
begin
|
||||
End;
|
||||
|
||||
Begin
|
||||
A := Buf[0];
|
||||
B := Buf[1];
|
||||
C := Buf[2];
|
||||
|
@ -281,20 +313,20 @@ begin
|
|||
Inc (Buf[1], B);
|
||||
Inc (Buf[2], C);
|
||||
Inc (Buf[3], D);
|
||||
end;
|
||||
End;
|
||||
|
||||
function MDFinal(var MDContext: TMDCtx; transform: TMDTransform): string;
|
||||
var
|
||||
Function MDFinal (Var MDContext: TMDCtx; Transform: TMDTransform) : String;
|
||||
Var
|
||||
Cnt : Word;
|
||||
P : Byte;
|
||||
digest: array[0..15] of Byte;
|
||||
i: Integer;
|
||||
n: integer;
|
||||
begin
|
||||
for I := 0 to 15 do
|
||||
Digest : Array[0..15] of Byte;
|
||||
I : Integer;
|
||||
N : Integer;
|
||||
Begin
|
||||
For I := 0 to 15 Do
|
||||
Digest[I] := I + 1;
|
||||
|
||||
with MDContext do begin
|
||||
With MDContext Do Begin
|
||||
Cnt := (Count[0] shr 3) and $3F;
|
||||
P := Cnt;
|
||||
BufAnsiChar[P] := $80;
|
||||
|
@ -309,16 +341,16 @@ begin
|
|||
Transform(State, BufLong);
|
||||
ArrLongToByte(BufLong, BufAnsiChar);
|
||||
|
||||
for n := 0 to 55 do
|
||||
BufAnsiChar[n] := 0;
|
||||
For N := 0 to 55 Do
|
||||
BufAnsiChar[N] := 0;
|
||||
|
||||
ArrByteToLong(BufAnsiChar, BufLong);
|
||||
end else begin
|
||||
for n := 0 to Cnt - 8 - 1 do
|
||||
End Else Begin
|
||||
For N := 0 to Cnt - 8 - 1 Do
|
||||
BufAnsiChar[p + n] := 0;
|
||||
|
||||
ArrByteToLong(BufAnsiChar, BufLong);
|
||||
end;
|
||||
End;
|
||||
|
||||
BufLong[14] := Count[0];
|
||||
BufLong[15] := Count[1];
|
||||
|
@ -328,49 +360,49 @@ begin
|
|||
|
||||
Result := '';
|
||||
|
||||
for i := 0 to 15 do
|
||||
Result := Result + char(digest[i]);
|
||||
end;
|
||||
end;
|
||||
For I := 0 to 15 Do
|
||||
Result := Result + Char(Digest[I]);
|
||||
End;
|
||||
End;
|
||||
|
||||
function MD5(const Value: string): string;
|
||||
var
|
||||
Function MD5 (Const Value: String): String;
|
||||
Var
|
||||
MDContext: TMDCtx;
|
||||
begin
|
||||
Begin
|
||||
MDInit (MDContext);
|
||||
MDUpdate (MDContext, Value, @MD5Transform);
|
||||
|
||||
Result := MDFinal(MDContext, @MD5Transform);
|
||||
end;
|
||||
End;
|
||||
|
||||
function HMAC_MD5(Text, Key: string): string;
|
||||
var
|
||||
Function HMAC_MD5(Text, Key: string): string;
|
||||
Var
|
||||
ipad, opad, s: string;
|
||||
n: Integer;
|
||||
MDContext: TMDCtx;
|
||||
begin
|
||||
if Length(Key) > 64 then
|
||||
Begin
|
||||
If Length(Key) > 64 then
|
||||
Key := md5(Key);
|
||||
|
||||
ipad := StringOfChar(#$36, 64);
|
||||
opad := StringOfChar(#$5C, 64);
|
||||
|
||||
for n := 1 to Length(Key) do begin
|
||||
For n := 1 to Length(Key) do begin
|
||||
ipad[n] := char(Byte(ipad[n]) xor Byte(Key[n]));
|
||||
opad[n] := char(Byte(opad[n]) xor Byte(Key[n]));
|
||||
end;
|
||||
End;
|
||||
|
||||
MDInit(MDContext);
|
||||
MDUpdate(MDContext, ipad, @MD5Transform);
|
||||
MDUpdate(MDContext, Text, @MD5Transform);
|
||||
|
||||
s := MDFinal(MDContext, @MD5Transform);
|
||||
S := MDFinal(MDContext, @MD5Transform);
|
||||
|
||||
MDInit(MDContext);
|
||||
MDUpdate(MDContext, opad, @MD5Transform);
|
||||
MDUpdate(MDContext, s, @MD5Transform);
|
||||
|
||||
Result := MDFinal(MDContext, @MD5Transform);
|
||||
end;
|
||||
End;
|
||||
|
||||
end.
|
||||
End.
|
Loading…
Reference in New Issue