Jump to content

How to get Hash out of .torrent file ?


szafran

Recommended Posts

Hi

I'd like to know how can I get the Hash value from .torrent file ?

(in the form that uTorrent shows it; ie. AABBCCDD EEFF0011 22334455 66778899 AABBCCDD)

(note: i need the hash for all the files included in .torrent - not for each one of them separatelly)

I've searched the .torrent files for corresponding hex values, but it seems that I can't find it that way.

Best regards

Szafran

Link to comment
Share on other sites

Ok. I wrote something, and it works (only) in 90% of the time. It would be nice if someone could point out the error that I've made.

Here's my code:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SHA1, DateUtils;

type
TForm1 = class(TForm)
nazwaPliku: TEdit;
Button1: TButton;
infoLabel: TLabel;
procedure Button1Click(Sender: TObject);
function DekodujZnaki(Znak : char) : string;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.DekodujZnaki(Znak : char) : string;
const
TablicaHexow: Array[0..15] of char = '0123456789ABCDEF';

var
b : Byte;

begin
b := Byte(Znak);
Result := TablicaHexow[b shr 4] + TablicaHexow[b and $0F];
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FS, FS1, FS2 : TFileStream;
dane : array of byte;
i, j, z, pola, tmpPos : integer;
Context : TSHA1Context;
Digest : TSHA1Digest;
wynik, temp, tmp : string;

begin
if not(SHA1SelfTest) then
begin
infoLabel.Caption := 'Błąd !!! Test modułu hashującego się nie powiódł !!!';
Exit;
end;
FS := TFileStream.Create(nazwaPliku.Text, fmOpenRead);
SetLength(dane, FS.Size);
FS.Read(dane[0], FS.Size);
FreeAndNil(FS);
temp := '';
SetLength(temp, Length(dane));
for i := 0 to Length(dane) - 1 do
temp[i + 1] := char(dane[i]);
temp := Copy(temp, Pos('4:info', temp) + 6);
pola := 0;
i := 1;
repeat
if (temp[i] in ['d','l','i']) then
begin
if (temp[i] = 'i') then
begin
tmp := Copy(temp, i);
tmpPos := Pos('e', tmp);
i := i + tmpPos;
end
else
begin
pola := pola + 1;
i := i + 1;
end;
Continue;
end;
if (temp[i] = 'e') then
begin
pola := pola - 1;
i := i + 1;
Continue;
end;
if (temp[i] in ['0'..'9']) then
begin
tmp := Copy(temp, i);
tmpPos := Pos(':', tmp);
tmp := Copy(temp, i, tmpPos - 1);
i := i + strtoint(tmp) + 2 + (tmpPos - 2);
end;
until (pola < 0);
SetLength(temp, i - 2);
SHA1Init(Context);
SHA1Update(Context, @temp[1], Length(temp));
SHA1Final(Context, Digest);

temp := '';
for i := 0 to Length(Digest) - 1 do
temp := temp + DekodujZnaki(char(Digest[i]));
wynik := '';
for i := 0 to Length(temp) - 1 do
if ((i mod 8) = 0) then
wynik := wynik + ' ' + temp[i + 1]
else
wynik := wynik + temp[i + 1];
infoLabel.Caption := 'Hash: ' + wynik;
end;

end.

this code uses sha1.pas and tools.pas that can be found here: http://www.koders.com/delphi/fid872E48EC7C07A22D798357AEC0BABAC9E52BD5AE.aspx?s=SHA1

any help will be appreciated.

Best Regards

Szafran

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...