?
Solved

How to calculate the hash of a .torrent file ?

Posted on 2008-10-09
1
Medium Priority
?
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Introduction to Processes
Suggested Courses

770 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