Cleanup
This commit is contained in:
parent
5ff385bf74
commit
82cd604c2f
334
mdl/m_crypt.pas
334
mdl/m_crypt.pas
|
@ -4,8 +4,10 @@ Unit m_Crypt;
|
||||||
|
|
||||||
Interface
|
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 HMAC_MD5 (Text, Key: String) : String;
|
||||||
Function MD5 (Const Value: String) : String;
|
Function MD5 (Const Value: String) : String;
|
||||||
Function Digest2String (Digest: String) : String;
|
Function Digest2String (Digest: String) : String;
|
||||||
|
@ -28,41 +30,70 @@ Type
|
||||||
BufLong : array[0..15] of Integer;
|
BufLong : array[0..15] of Integer;
|
||||||
End;
|
End;
|
||||||
|
|
||||||
// rewrite the terribad functions now that this stuff is working
|
Const
|
||||||
|
B64Codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||||
|
|
||||||
function HexToByte (Hex: String) : byte;
|
Function B64Encode (S: String) : String;
|
||||||
var
|
Var
|
||||||
n, n2, sayi: Integer;
|
I : Integer;
|
||||||
|
A : Integer;
|
||||||
|
X : Integer;
|
||||||
|
B : Integer;
|
||||||
Begin
|
Begin
|
||||||
n := 0;
|
Result := '';
|
||||||
n2 := 0;
|
A := 0;
|
||||||
|
B := 0;
|
||||||
|
|
||||||
Case Hex[1] Of
|
For I := 1 to Length(S) Do Begin
|
||||||
'A','a': n:= 10;
|
X := Byte(S[I]);
|
||||||
'B','b': n:= 11;
|
B := B * 256 + X;
|
||||||
'C','c': n:= 12;
|
A := A + 8;
|
||||||
'D','d': n:= 13;
|
|
||||||
'E','e': n:= 14;
|
While A >= 6 Do Begin
|
||||||
'F','f': n:= 15;
|
A := A - 6;
|
||||||
|
X := B DIV (1 SHL a);
|
||||||
|
B := B MOD (1 SHL a);
|
||||||
|
Result := Result + B64Codes[X + 1];
|
||||||
|
End;
|
||||||
End;
|
End;
|
||||||
|
|
||||||
If Hex[1] in ['0','1','2','3','4','5','6','7','8','9'] Then
|
If A > 0 Then Begin
|
||||||
n := strS2I(Hex[1]);
|
X := B SHL (6 - A);
|
||||||
|
Result := Result + B64Codes[X + 1];
|
||||||
|
|
||||||
Case Hex[2] Of
|
If A = 4 Then Result := Result + '=';
|
||||||
'A','a': n2:= 10;
|
If A = 2 Then Result := Result + '==';
|
||||||
'B','b': n2:= 11;
|
|
||||||
'C','c': n2:= 12;
|
|
||||||
'D','d': n2:= 13;
|
|
||||||
'E','e': n2:= 14;
|
|
||||||
'F','f': n2:= 15;
|
|
||||||
End;
|
End;
|
||||||
|
End;
|
||||||
|
|
||||||
If Hex[2] in ['0','1','2','3','4','5','6','7','8','9'] Then
|
Function B64Decode (S: String) : String;
|
||||||
n2 := strS2I(Hex[2]);
|
Var
|
||||||
|
I : Integer;
|
||||||
|
A : Integer;
|
||||||
|
X : Integer;
|
||||||
|
B : Integer;
|
||||||
|
Begin
|
||||||
|
Result := '';
|
||||||
|
A := 0;
|
||||||
|
B := 0;
|
||||||
|
|
||||||
sayi := n * 16 + n2;
|
For I := 1 to Length(S) Do Begin
|
||||||
Result := sayi;
|
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;
|
End;
|
||||||
|
|
||||||
Function Digest2String (Digest: String) : String;
|
Function Digest2String (Digest: String) : String;
|
||||||
|
@ -85,7 +116,7 @@ Begin
|
||||||
Count := 1;
|
Count := 1;
|
||||||
|
|
||||||
While Count < Length(Str) Do Begin
|
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);
|
Inc (Count, 2);
|
||||||
End;
|
End;
|
||||||
End;
|
End;
|
||||||
|
@ -136,12 +167,12 @@ Begin
|
||||||
|
|
||||||
Inc(Count[0], InputLen shl 3);
|
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]);
|
||||||
|
|
||||||
Inc(Count[1], InputLen shr 29);
|
Inc(Count[1], InputLen shr 29);
|
||||||
|
|
||||||
partLen := 64 - Index;
|
PartLen := 64 - Index;
|
||||||
|
|
||||||
if InputLen >= partLen then begin
|
if InputLen >= partLen then begin
|
||||||
ArrLongToByte(BufLong, BufAnsiChar);
|
ArrLongToByte(BufLong, BufAnsiChar);
|
||||||
|
@ -163,138 +194,139 @@ Begin
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Index := 0;
|
Index := 0;
|
||||||
end else
|
End Else
|
||||||
I := 0;
|
I := 0;
|
||||||
|
|
||||||
ArrLongToByte(BufLong, BufAnsiChar);
|
ArrLongToByte(BufLong, BufAnsiChar);
|
||||||
Move(Data[I+1], BufAnsiChar[Index], InputLen-I);
|
Move(Data[I+1], BufAnsiChar[Index], InputLen-I);
|
||||||
ArrByteToLong(BufAnsiChar, BufLong);
|
ArrByteToLong(BufAnsiChar, BufLong);
|
||||||
end
|
End
|
||||||
end;
|
End;
|
||||||
|
|
||||||
procedure MD5Transform(var Buf: array of LongInt; const Data: array of LongInt);
|
Procedure MD5Transform (Var Buf: Array of LongInt; const Data: Array of LongInt);
|
||||||
var
|
Var
|
||||||
A, B, C, D: LongInt;
|
A, B, C, D: LongInt;
|
||||||
|
|
||||||
procedure Round1(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
Procedure Round1 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||||
begin
|
Begin
|
||||||
Inc(W, (Z xor (X and (Y xor Z))) + Data);
|
Inc(W, (Z xor (X and (Y xor Z))) + Data);
|
||||||
W := (W shl S) or (W shr (32 - S));
|
W := (W shl S) or (W shr (32 - S));
|
||||||
Inc(W, X);
|
Inc(W, X);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
procedure Round2(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
Procedure Round2 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||||
begin
|
Begin
|
||||||
Inc(W, (Y xor (Z and (X xor Y))) + Data);
|
Inc(W, (Y xor (Z and (X xor Y))) + Data);
|
||||||
W := (W shl S) or (W shr (32 - S));
|
W := (W shl S) or (W shr (32 - S));
|
||||||
Inc(W, X);
|
Inc(W, X);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
procedure Round3(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
Procedure Round3 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||||
begin
|
Begin
|
||||||
Inc(W, (X xor Y xor Z) + Data);
|
Inc(W, (X xor Y xor Z) + Data);
|
||||||
W := (W shl S) or (W shr (32 - S));
|
W := (W shl S) or (W shr (32 - S));
|
||||||
Inc(W, X);
|
Inc(W, X);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
procedure Round4(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
Procedure Round4 (Var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
|
||||||
begin
|
Begin
|
||||||
Inc(W, (Y xor (X or not Z)) + Data);
|
Inc(W, (Y xor (X or not Z)) + Data);
|
||||||
W := (W shl S) or (W shr (32 - S));
|
W := (W shl S) or (W shr (32 - S));
|
||||||
Inc(W, X);
|
Inc(W, X);
|
||||||
end;
|
End;
|
||||||
begin
|
|
||||||
|
Begin
|
||||||
A := Buf[0];
|
A := Buf[0];
|
||||||
B := Buf[1];
|
B := Buf[1];
|
||||||
C := Buf[2];
|
C := Buf[2];
|
||||||
D := Buf[3];
|
D := Buf[3];
|
||||||
|
|
||||||
Round1(A, B, C, D, Data[0] + Longint($D76AA478), 7);
|
Round1 (A, B, C, D, Data[0] + Longint($D76AA478), 7);
|
||||||
Round1(D, A, B, C, Data[1] + Longint($E8C7B756), 12);
|
Round1 (D, A, B, C, Data[1] + Longint($E8C7B756), 12);
|
||||||
Round1(C, D, A, B, Data[2] + Longint($242070DB), 17);
|
Round1 (C, D, A, B, Data[2] + Longint($242070DB), 17);
|
||||||
Round1(B, C, D, A, Data[3] + Longint($C1BDCEEE), 22);
|
Round1 (B, C, D, A, Data[3] + Longint($C1BDCEEE), 22);
|
||||||
Round1(A, B, C, D, Data[4] + Longint($F57C0FAF), 7);
|
Round1 (A, B, C, D, Data[4] + Longint($F57C0FAF), 7);
|
||||||
Round1(D, A, B, C, Data[5] + Longint($4787C62A), 12);
|
Round1 (D, A, B, C, Data[5] + Longint($4787C62A), 12);
|
||||||
Round1(C, D, A, B, Data[6] + Longint($A8304613), 17);
|
Round1 (C, D, A, B, Data[6] + Longint($A8304613), 17);
|
||||||
Round1(B, C, D, A, Data[7] + Longint($FD469501), 22);
|
Round1 (B, C, D, A, Data[7] + Longint($FD469501), 22);
|
||||||
Round1(A, B, C, D, Data[8] + Longint($698098D8), 7);
|
Round1 (A, B, C, D, Data[8] + Longint($698098D8), 7);
|
||||||
Round1(D, A, B, C, Data[9] + Longint($8B44F7AF), 12);
|
Round1 (D, A, B, C, Data[9] + Longint($8B44F7AF), 12);
|
||||||
Round1(C, D, A, B, Data[10] + Longint($FFFF5BB1), 17);
|
Round1 (C, D, A, B, Data[10] + Longint($FFFF5BB1), 17);
|
||||||
Round1(B, C, D, A, Data[11] + Longint($895CD7BE), 22);
|
Round1 (B, C, D, A, Data[11] + Longint($895CD7BE), 22);
|
||||||
Round1(A, B, C, D, Data[12] + Longint($6B901122), 7);
|
Round1 (A, B, C, D, Data[12] + Longint($6B901122), 7);
|
||||||
Round1(D, A, B, C, Data[13] + Longint($FD987193), 12);
|
Round1 (D, A, B, C, Data[13] + Longint($FD987193), 12);
|
||||||
Round1(C, D, A, B, Data[14] + Longint($A679438E), 17);
|
Round1 (C, D, A, B, Data[14] + Longint($A679438E), 17);
|
||||||
Round1(B, C, D, A, Data[15] + Longint($49B40821), 22);
|
Round1 (B, C, D, A, Data[15] + Longint($49B40821), 22);
|
||||||
|
|
||||||
Round2(A, B, C, D, Data[1] + Longint($F61E2562), 5);
|
Round2 (A, B, C, D, Data[1] + Longint($F61E2562), 5);
|
||||||
Round2(D, A, B, C, Data[6] + Longint($C040B340), 9);
|
Round2 (D, A, B, C, Data[6] + Longint($C040B340), 9);
|
||||||
Round2(C, D, A, B, Data[11] + Longint($265E5A51), 14);
|
Round2 (C, D, A, B, Data[11] + Longint($265E5A51), 14);
|
||||||
Round2(B, C, D, A, Data[0] + Longint($E9B6C7AA), 20);
|
Round2 (B, C, D, A, Data[0] + Longint($E9B6C7AA), 20);
|
||||||
Round2(A, B, C, D, Data[5] + Longint($D62F105D), 5);
|
Round2 (A, B, C, D, Data[5] + Longint($D62F105D), 5);
|
||||||
Round2(D, A, B, C, Data[10] + Longint($02441453), 9);
|
Round2 (D, A, B, C, Data[10] + Longint($02441453), 9);
|
||||||
Round2(C, D, A, B, Data[15] + Longint($D8A1E681), 14);
|
Round2 (C, D, A, B, Data[15] + Longint($D8A1E681), 14);
|
||||||
Round2(B, C, D, A, Data[4] + Longint($E7D3FBC8), 20);
|
Round2 (B, C, D, A, Data[4] + Longint($E7D3FBC8), 20);
|
||||||
Round2(A, B, C, D, Data[9] + Longint($21E1CDE6), 5);
|
Round2 (A, B, C, D, Data[9] + Longint($21E1CDE6), 5);
|
||||||
Round2(D, A, B, C, Data[14] + Longint($C33707D6), 9);
|
Round2 (D, A, B, C, Data[14] + Longint($C33707D6), 9);
|
||||||
Round2(C, D, A, B, Data[3] + Longint($F4D50D87), 14);
|
Round2 (C, D, A, B, Data[3] + Longint($F4D50D87), 14);
|
||||||
Round2(B, C, D, A, Data[8] + Longint($455A14ED), 20);
|
Round2 (B, C, D, A, Data[8] + Longint($455A14ED), 20);
|
||||||
Round2(A, B, C, D, Data[13] + Longint($A9E3E905), 5);
|
Round2 (A, B, C, D, Data[13] + Longint($A9E3E905), 5);
|
||||||
Round2(D, A, B, C, Data[2] + Longint($FCEFA3F8), 9);
|
Round2 (D, A, B, C, Data[2] + Longint($FCEFA3F8), 9);
|
||||||
Round2(C, D, A, B, Data[7] + Longint($676F02D9), 14);
|
Round2 (C, D, A, B, Data[7] + Longint($676F02D9), 14);
|
||||||
Round2(B, C, D, A, Data[12] + Longint($8D2A4C8A), 20);
|
Round2 (B, C, D, A, Data[12] + Longint($8D2A4C8A), 20);
|
||||||
|
|
||||||
Round3(A, B, C, D, Data[5] + Longint($FFFA3942), 4);
|
Round3 (A, B, C, D, Data[5] + Longint($FFFA3942), 4);
|
||||||
Round3(D, A, B, C, Data[8] + Longint($8771F681), 11);
|
Round3 (D, A, B, C, Data[8] + Longint($8771F681), 11);
|
||||||
Round3(C, D, A, B, Data[11] + Longint($6D9D6122), 16);
|
Round3 (C, D, A, B, Data[11] + Longint($6D9D6122), 16);
|
||||||
Round3(B, C, D, A, Data[14] + Longint($FDE5380C), 23);
|
Round3 (B, C, D, A, Data[14] + Longint($FDE5380C), 23);
|
||||||
Round3(A, B, C, D, Data[1] + Longint($A4BEEA44), 4);
|
Round3 (A, B, C, D, Data[1] + Longint($A4BEEA44), 4);
|
||||||
Round3(D, A, B, C, Data[4] + Longint($4BDECFA9), 11);
|
Round3 (D, A, B, C, Data[4] + Longint($4BDECFA9), 11);
|
||||||
Round3(C, D, A, B, Data[7] + Longint($F6BB4B60), 16);
|
Round3 (C, D, A, B, Data[7] + Longint($F6BB4B60), 16);
|
||||||
Round3(B, C, D, A, Data[10] + Longint($BEBFBC70), 23);
|
Round3 (B, C, D, A, Data[10] + Longint($BEBFBC70), 23);
|
||||||
Round3(A, B, C, D, Data[13] + Longint($289B7EC6), 4);
|
Round3 (A, B, C, D, Data[13] + Longint($289B7EC6), 4);
|
||||||
Round3(D, A, B, C, Data[0] + Longint($EAA127FA), 11);
|
Round3 (D, A, B, C, Data[0] + Longint($EAA127FA), 11);
|
||||||
Round3(C, D, A, B, Data[3] + Longint($D4EF3085), 16);
|
Round3 (C, D, A, B, Data[3] + Longint($D4EF3085), 16);
|
||||||
Round3(B, C, D, A, Data[6] + Longint($04881D05), 23);
|
Round3 (B, C, D, A, Data[6] + Longint($04881D05), 23);
|
||||||
Round3(A, B, C, D, Data[9] + Longint($D9D4D039), 4);
|
Round3 (A, B, C, D, Data[9] + Longint($D9D4D039), 4);
|
||||||
Round3(D, A, B, C, Data[12] + Longint($E6DB99E5), 11);
|
Round3 (D, A, B, C, Data[12] + Longint($E6DB99E5), 11);
|
||||||
Round3(C, D, A, B, Data[15] + Longint($1FA27CF8), 16);
|
Round3 (C, D, A, B, Data[15] + Longint($1FA27CF8), 16);
|
||||||
Round3(B, C, D, A, Data[2] + Longint($C4AC5665), 23);
|
Round3 (B, C, D, A, Data[2] + Longint($C4AC5665), 23);
|
||||||
|
|
||||||
Round4(A, B, C, D, Data[0] + Longint($F4292244), 6);
|
Round4 (A, B, C, D, Data[0] + Longint($F4292244), 6);
|
||||||
Round4(D, A, B, C, Data[7] + Longint($432AFF97), 10);
|
Round4 (D, A, B, C, Data[7] + Longint($432AFF97), 10);
|
||||||
Round4(C, D, A, B, Data[14] + Longint($AB9423A7), 15);
|
Round4 (C, D, A, B, Data[14] + Longint($AB9423A7), 15);
|
||||||
Round4(B, C, D, A, Data[5] + Longint($FC93A039), 21);
|
Round4 (B, C, D, A, Data[5] + Longint($FC93A039), 21);
|
||||||
Round4(A, B, C, D, Data[12] + Longint($655B59C3), 6);
|
Round4 (A, B, C, D, Data[12] + Longint($655B59C3), 6);
|
||||||
Round4(D, A, B, C, Data[3] + Longint($8F0CCC92), 10);
|
Round4 (D, A, B, C, Data[3] + Longint($8F0CCC92), 10);
|
||||||
Round4(C, D, A, B, Data[10] + Longint($FFEFF47D), 15);
|
Round4 (C, D, A, B, Data[10] + Longint($FFEFF47D), 15);
|
||||||
Round4(B, C, D, A, Data[1] + Longint($85845DD1), 21);
|
Round4 (B, C, D, A, Data[1] + Longint($85845DD1), 21);
|
||||||
Round4(A, B, C, D, Data[8] + Longint($6FA87E4F), 6);
|
Round4 (A, B, C, D, Data[8] + Longint($6FA87E4F), 6);
|
||||||
Round4(D, A, B, C, Data[15] + Longint($FE2CE6E0), 10);
|
Round4 (D, A, B, C, Data[15] + Longint($FE2CE6E0), 10);
|
||||||
Round4(C, D, A, B, Data[6] + Longint($A3014314), 15);
|
Round4 (C, D, A, B, Data[6] + Longint($A3014314), 15);
|
||||||
Round4(B, C, D, A, Data[13] + Longint($4E0811A1), 21);
|
Round4 (B, C, D, A, Data[13] + Longint($4E0811A1), 21);
|
||||||
Round4(A, B, C, D, Data[4] + Longint($F7537E82), 6);
|
Round4 (A, B, C, D, Data[4] + Longint($F7537E82), 6);
|
||||||
Round4(D, A, B, C, Data[11] + Longint($BD3AF235), 10);
|
Round4 (D, A, B, C, Data[11] + Longint($BD3AF235), 10);
|
||||||
Round4(C, D, A, B, Data[2] + Longint($2AD7D2BB), 15);
|
Round4 (C, D, A, B, Data[2] + Longint($2AD7D2BB), 15);
|
||||||
Round4(B, C, D, A, Data[9] + Longint($EB86D391), 21);
|
Round4 (B, C, D, A, Data[9] + Longint($EB86D391), 21);
|
||||||
|
|
||||||
Inc(Buf[0], A);
|
Inc (Buf[0], A);
|
||||||
Inc(Buf[1], B);
|
Inc (Buf[1], B);
|
||||||
Inc(Buf[2], C);
|
Inc (Buf[2], C);
|
||||||
Inc(Buf[3], D);
|
Inc (Buf[3], D);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
function MDFinal(var MDContext: TMDCtx; transform: TMDTransform): string;
|
Function MDFinal (Var MDContext: TMDCtx; Transform: TMDTransform) : String;
|
||||||
var
|
Var
|
||||||
Cnt: Word;
|
Cnt : Word;
|
||||||
P: Byte;
|
P : Byte;
|
||||||
digest: array[0..15] of Byte;
|
Digest : Array[0..15] of Byte;
|
||||||
i: Integer;
|
I : Integer;
|
||||||
n: integer;
|
N : Integer;
|
||||||
begin
|
Begin
|
||||||
for I := 0 to 15 do
|
For I := 0 to 15 Do
|
||||||
Digest[I] := I + 1;
|
Digest[I] := I + 1;
|
||||||
|
|
||||||
with MDContext do begin
|
With MDContext Do Begin
|
||||||
Cnt := (Count[0] shr 3) and $3F;
|
Cnt := (Count[0] shr 3) and $3F;
|
||||||
P := Cnt;
|
P := Cnt;
|
||||||
BufAnsiChar[P] := $80;
|
BufAnsiChar[P] := $80;
|
||||||
|
@ -309,16 +341,16 @@ begin
|
||||||
Transform(State, BufLong);
|
Transform(State, BufLong);
|
||||||
ArrLongToByte(BufLong, BufAnsiChar);
|
ArrLongToByte(BufLong, BufAnsiChar);
|
||||||
|
|
||||||
for n := 0 to 55 do
|
For N := 0 to 55 Do
|
||||||
BufAnsiChar[n] := 0;
|
BufAnsiChar[N] := 0;
|
||||||
|
|
||||||
ArrByteToLong(BufAnsiChar, BufLong);
|
ArrByteToLong(BufAnsiChar, BufLong);
|
||||||
end else begin
|
End Else Begin
|
||||||
for n := 0 to Cnt - 8 - 1 do
|
For N := 0 to Cnt - 8 - 1 Do
|
||||||
BufAnsiChar[p + n] := 0;
|
BufAnsiChar[p + n] := 0;
|
||||||
|
|
||||||
ArrByteToLong(BufAnsiChar, BufLong);
|
ArrByteToLong(BufAnsiChar, BufLong);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
BufLong[14] := Count[0];
|
BufLong[14] := Count[0];
|
||||||
BufLong[15] := Count[1];
|
BufLong[15] := Count[1];
|
||||||
|
@ -328,49 +360,49 @@ begin
|
||||||
|
|
||||||
Result := '';
|
Result := '';
|
||||||
|
|
||||||
for i := 0 to 15 do
|
For I := 0 to 15 Do
|
||||||
Result := Result + char(digest[i]);
|
Result := Result + Char(Digest[I]);
|
||||||
end;
|
End;
|
||||||
end;
|
End;
|
||||||
|
|
||||||
function MD5(const Value: string): string;
|
Function MD5 (Const Value: String): String;
|
||||||
var
|
Var
|
||||||
MDContext: TMDCtx;
|
MDContext: TMDCtx;
|
||||||
begin
|
Begin
|
||||||
MDInit(MDContext);
|
MDInit (MDContext);
|
||||||
MDUpdate(MDContext, Value, @MD5Transform);
|
MDUpdate (MDContext, Value, @MD5Transform);
|
||||||
|
|
||||||
Result := MDFinal(MDContext, @MD5Transform);
|
Result := MDFinal(MDContext, @MD5Transform);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
function HMAC_MD5(Text, Key: string): string;
|
Function HMAC_MD5(Text, Key: string): string;
|
||||||
var
|
Var
|
||||||
ipad, opad, s: string;
|
ipad, opad, s: string;
|
||||||
n: Integer;
|
n: Integer;
|
||||||
MDContext: TMDCtx;
|
MDContext: TMDCtx;
|
||||||
begin
|
Begin
|
||||||
if Length(Key) > 64 then
|
If Length(Key) > 64 then
|
||||||
Key := md5(Key);
|
Key := md5(Key);
|
||||||
|
|
||||||
ipad := StringOfChar(#$36, 64);
|
ipad := StringOfChar(#$36, 64);
|
||||||
opad := StringOfChar(#$5C, 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]));
|
ipad[n] := char(Byte(ipad[n]) xor Byte(Key[n]));
|
||||||
opad[n] := char(Byte(opad[n]) xor Byte(Key[n]));
|
opad[n] := char(Byte(opad[n]) xor Byte(Key[n]));
|
||||||
end;
|
End;
|
||||||
|
|
||||||
MDInit(MDContext);
|
MDInit(MDContext);
|
||||||
MDUpdate(MDContext, ipad, @MD5Transform);
|
MDUpdate(MDContext, ipad, @MD5Transform);
|
||||||
MDUpdate(MDContext, Text, @MD5Transform);
|
MDUpdate(MDContext, Text, @MD5Transform);
|
||||||
|
|
||||||
s := MDFinal(MDContext, @MD5Transform);
|
S := MDFinal(MDContext, @MD5Transform);
|
||||||
|
|
||||||
MDInit(MDContext);
|
MDInit(MDContext);
|
||||||
MDUpdate(MDContext, opad, @MD5Transform);
|
MDUpdate(MDContext, opad, @MD5Transform);
|
||||||
MDUpdate(MDContext, s, @MD5Transform);
|
MDUpdate(MDContext, s, @MD5Transform);
|
||||||
|
|
||||||
Result := MDFinal(MDContext, @MD5Transform);
|
Result := MDFinal(MDContext, @MD5Transform);
|
||||||
end;
|
End;
|
||||||
|
|
||||||
end.
|
End.
|
Loading…
Reference in New Issue