How to get Hash out of .torrent file ? (Page 1) / Protocol Design Discussion / µTorrent Community Forums
 

#1 2008-10-08 15:37:45

szafran
Member

How to get Hash out of .torrent file ?

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

Offline

#2 2008-10-08 15:53:39

DreadWingKnight
I never claimed to be nice.

Re: How to get Hash out of .torrent file ?


FAQ and Search - Use them.
If guides ask for info, provide it before I have to ask for it.

Online

#3 2008-10-08 16:06:24

Ultima
Administrator

Re: How to get Hash out of .torrent file ?

You need to get the SHA1 hash of the info dictionary in the .torrent metadata file.


[size=0.85][ Tweaking Checklist | User Manual | BEE | MiniUI | µTA ][/size]

Offline

#4 2008-10-08 16:43:17

szafran
Member

Re: How to get Hash out of .torrent file ?

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/fid872E48E … spx?s=SHA1

any help will be appreciated.

Best Regards
Szafran

Last edited by szafran (2008-10-12 00:01:30)

Offline

Board footer

Powered by FluxBB

©2013 BitTorrent, Inc. µTorrent is a trademark of BitTorrent, Inc.