?
Solved

How to calculate the hash of a .torrent file ?

Posted on 2008-10-09
1
Medium Priority
?
1,145 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Simple Linear Regression
Loops Section Overview

862 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