Solved

How to calculate the hash of a .torrent file ?

Posted on 2008-10-09
1
1,086 Views
Last Modified: 2013-11-22
Hi

I'm trying to calculate hash of a .torrent file, and can't seem to get it to work. What I'm getting is far from what the result should be. I'm using the SHA code from here: http://www.koders.com/delphi/fid872E48EC7C07A22D798357AEC0BABAC9E52BD5AE.aspx?s=sha1 (sha1.pas and tools.pas). And here is my code:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SHA1;

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 : TFileStream;
  dane : array of byte;
  i, j : integer;
  Context : TSHA1Context;
  Digest : TSHA1Digest;
  wynik : string;

begin
  if not(SHA1SelfTest) then
    begin
      infoLabel.Caption := 'BBd !!! Test moduBu hashujcego si nie powiódB !!!';
      Exit;
    end;
  FS := TFileStream.Create(nazwaPliku.Text, fmOpenRead);
  SetLength(dane, FS.Size);
  for i := 0 to Length(dane) - 1 do
    FS.Read(dane[i], 1);
  FreeAndNil(FS);
  for i := 0 to Length(dane) - 1 do
    if ((char(dane[i]) = '4') AND (char(dane[i + 1]) = ':') AND
        (char(dane[i + 2]) = 'i') AND (char(dane[i + 3]) = 'n') AND
        (char(dane[i + 4]) = 'f') AND (char(dane[i + 5]) = 'o')) then
            begin
              for j := 0 to Length(dane) - 1 do
                dane[j] := dane[i + j];
              SetLength(dane, Length(dane) - i);
              Break;
            end;

  SHA1Init(Context);
  SHA1Update(Context, @dane[1], length(dane));
  SHA1Final(Context, Digest);
  wynik := '';
  for i := 0 to Length(Digest) - 1 do
    wynik := wynik + DekodujZnaki(char(Digest[i]));
  infoLabel.Caption := 'Hash: ' + wynik;
end;

end.

Any help would be appreciated.
0
Comment
Question by:tadeusz81
1 Comment
 

Accepted Solution

by:
tadeusz81 earned 0 total points
ID: 22682653
this Q can be closed now... I got it to work... code was written ok, except for one little error

this line: SHA1Update(Context, @dane[1], length(dane));

should be: SHA1Update(Context, @dane[0], length(dane));

and it works great.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now