Solved

How to calculate the hash of a .torrent file ?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression

690 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