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
|
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var s: string;
begin
s := Socket.ReceiveText;
Memo1.Lines.Add(s);
envoyerFichier(socket, s);
end;
procedure TForm1.envoyerFichier(Socket: TCustomWinSocket; nomFichier: String);
var md5rep, s, data: string;
begin
md5rep := decodeEntete(nomFichier);
s := 'HTTP/1.1 101 WebSocket Protocol Handshake' + #13#10
+ 'Upgrade: WebSocket' + #13#10
+ 'Connection: Upgrade' + #13#10
+ 'Sec-WebSocket-Location: ws://exemple../' + #13#10
+ 'Sec-WebSocket-Origin: http://localhost' + #13#10
+ 'Sec-WebSocket-Protocol: sample' + #13#10
+ #13#10 + md5rep;
Memo1.Lines.Add(s);
Socket.SendText(s);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Open;
end;
function TForm1.decodeEntete(s:string):string;
var i: integer;
key1, key2, data: string;
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]
else if(i=9)then data := Memo1.Lines[i];
end;
key1 := copy(key1, 21, length(key1));
key2 := copy(key2, 21, length(key2));
Memo1.Lines.Add(#13#10+'key1: '+key1+#13#10+'key2: '+key2+#13#10+'key3: '+data+#13#10);
if not(length(data) <> 8) then
result := Hixie76Signature(key1, key2, data)
else Memo1.Lines.Add('bad plus de 8');
end;
function Tform1.Hixie76Signature(key1, key2, data: string): string;
var test, n1, n2, totalKey: string;
i, s1, s2: integer;
p1, p2: int64;
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;
n1 := '';
for i := 0 to 3 do
begin
n1 := chr((p1 shr ((i and 7) shl 3)) and $ff) + n1; // convertion binaire sous 32bits
end;
n2 := '';
for i := 0 to 3 do
begin
n2 := chr((p2 shr ((i and 7) shl 3)) and $ff) + n2;
end;
totalKey := n1 + n2 + UTF8Decode(data);
result := md5(totalKey);
end;
function TForm1.md5(const Input: String): String;
var
hCryptProvider: HCRYPTPROV;
hHash: HCRYPTHASH;
bHash: array[0..$7f] of Byte;
dwHashBytes: Cardinal;
pbContent: PByte;
i: Integer;
begin
dwHashBytes := 16;
pbContent := Pointer(PChar(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(Char), 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; |
Partager