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
| unit FrmFirstProjectMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Vcl.StdCtrls, wcrypt2;
type
TFirstProjectMain = class(TForm)
Button1: TButton;
Memo1: TMemo;
ButtonTest: TButton;
procedure Button1Click(Sender: TObject);
procedure ButtonTestClick(Sender: TObject);
private
{ Déclarations privées }
function decodeEntete(s:Ansistring):Ansistring;
function Hixie76Signature(key1, key2, data: Ansistring): Ansistring;
function md5(const Input: Ansistring): Ansistring; public
{ Déclarations publiques }
end;
var
FirstProjectMain: TFirstProjectMain;
implementation
{$R *.dfm}
function SwapLongword(Value: Longword): Longword;
asm
BSWAP EAX
end;
procedure TFirstProjectMain.Button1Click(Sender: TObject);
var
p1, p2: LongWord;
begin
p1 := 123;
p2 := 456;
p1 := SwapLongword(p1);
p2 := SwapLongword(p2);
ShowMessage(IntToStr(p1));
ShowMessage(IntToStr(p2));
end;
function TFirstProjectMain.decodeEntete(s:Ansistring):Ansistring;
var i: integer;
key1, key2, data: Ansistring;
host, origin: Ansistring;
begin
data := ''; key1 := ''; key2:='';
For i := 1 to 10 do begin
if(pos('Sec-WebSocket-Key1: ', Memo1.Lines[i]) > 0) then
key1 := Memo1.Lines[i]
else if(pos('Sec-WebSocket-Key2: ', Memo1.Lines[i]) > 0) then
key2 := Memo1.Lines[i];
case i of
3: host := copy(Memo1.Lines[i], 7, length(Memo1.Lines[i]));
4: origin := copy(Memo1.Lines[i], 9, length(Memo1.Lines[i]));
end;
end;
data := copy(s, Length(s) - 8 + 1, 8);
key1 := copy(key1, 21, length(key1));
key2 := copy(key2, 21, length(key2));
result := Hixie76Signature(key1, key2, data);
end;
procedure TFirstProjectMain.ButtonTestClick(Sender: TObject);
var s: ansistring;
begin
s := 'GET /demo HTTP/1.1' + #13#10 +
'Host: example.com' + #13#10 +
'Connection: Upgrade' + #13#10 +
'Sec-WebSocket-Key2: 12998 5 Y3 1 .P00' + #13#10 +
'Sec-WebSocket-Protocol: sample' + #13#10 +
'Upgrade: WebSocket' + #13#10 +
'Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5' + #13#10 +
'Origin: http://example.com' + #13#10
+ #13#10 +
'^n:ds[4U';
Memo1.Lines.Text := s;
ShowMessage(decodeEntete(s) + ' doit être : "8jKS''y:G*Co,Wxa-"');
end;
function TFirstProjectMain.Hixie76Signature(key1, key2, data: Ansistring): Ansistring;
var test, n1, n2, totalKey: Ansistring;
i, s1, s2: integer;
p1, p2: LongWord;
resultHexa: Ansistring;
Token : Ansistring;
PToken: PAnsiChar;
begin
result := '';
s1 := 0;
n1 := '';
s2 := 0;
n2 := '';
for i := 1 to length(key1) do
begin
if ((key1[i] >= '0') and (key1[i] <= '9')) then
n1 := n1 + key1[i];
if (key1[i] = ' ') then
inc(s1);
end;
for i := 1 to length(key2) do
begin
if ((key2[i] >= '0') and (key2[i] <= '9')) then
n2 := n2 + key2[i];
if (key2[i] = ' ') then
inc(s2);
end;
p1 := StrToInt64(n1) div s1;
p2 := StrToInt64(n2) div s2;
p1 := SwapLongword(p1);
p2 := SwapLongword(p2);
SetLength(Token, 16);
PToken := PAnsiChar(Token);
CopyMemory(PToken, @p1, 4);
Inc(PToken, 4);
CopyMemory(PToken, @p2, 4);
Inc(PToken, 4);
CopyMemory(PToken, PAnsiChar(data), 8);
resultHexa := md5(Token);
// Hexa -> Binaire, MD5 ne fourni que des digits de 0 à 7F, pas besoin d'UTF8
SetLength(Result, 16);
HexToBin(Pansichar(resultHexa), Pansichar(Result), 16);
end;
function TFirstProjectMain.md5(const Input: Ansistring): Ansistring;
var
hCryptProvider: HCRYPTPROV;
hHash: HCRYPTHASH;
bHash: array[0..$7f] of Byte;
dwHashBytes: Cardinal;
pbContent: PByte;
i: Integer;
begin
dwHashBytes := 16;
pbContent := Pointer(PAnsiChar(Input));
Result := '';
if CryptAcquireContext(@hCryptProvider, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT or CRYPT_MACHINE_KEYSET) then
begin
if CryptCreateHash(hCryptProvider, CALG_MD5, 0, 0, @hHash) then
begin
if CryptHashData(hHash, pbContent, Length(Input) * sizeof(AnsiChar), 0) then
begin
if CryptGetHashParam(hHash, HP_HASHVAL, @bHash[0], @dwHashBytes, 0) then
begin
for i := 0 to dwHashBytes - 1 do
begin
Result := Result + Format('%.2x', [bHash[i]]);
end;
end;
end;
CryptDestroyHash(hHash);
end;
CryptReleaseContext(hCryptProvider, 0);
end;
Result := AnsiLowerCase(Result);
end;
end. |