Solved

How to calculate the hash of a .torrent file ?

Posted on 2008-10-09
1
1,089 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

914 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

19 Experts available now in Live!

Get 1:1 Help Now