Skip to content

Commit 586edf7

Browse files
committed
1.10: hash torrent file
1 parent 105b261 commit 586edf7

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
PROJECT := torrentcheck
12
CFLAGS := -O
23

3-
all: torrentcheck
4+
all: $(PROJECT)
45

56
clean:
6-
rm *.o
7+
rm *.o $(PROJECT)
78

8-
torrentcheck: torrentcheck.o sha1.o
9+
$(PROJECT): torrentcheck.o sha1.o
910
$(CC) -o $@ $^ $(CFLAGS)

README renamed to README.md

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
torrentcheck - catalog a .torrent file and optionally verify content hashes
2-
Usage: torrentcheck -t torrent-file [-p content-path] [-n] [-h] [-c] [-d]
3-
Options: -n suppresses progress count, -h shows all hash values,
4-
-c or -d uses comma or dot formatted byte counts.
1+
# torrentcheck
2+
3+
torrentcheck - catalog a `.torrent` file and optionally verify content hashes.
4+
5+
Usage: `torrentcheck torrent-file [-p content-path] [-n] [-h] [-c] [-d]`
6+
7+
Options:
8+
`-n` suppresses progress count,
9+
`-h` shows all hash values,
10+
`-c` or `-d` uses comma or dot formatted byte counts.
11+
512
Returns 0 if successful, nonzero return code if errors found.
613

7-
Option: -sha1 [optional hash] acts as a simple SHA1 filter.
8-
If -sha1 is followed by a hex hash, the return code will be zero
14+
Option: `-sha1` [optional hash] acts as a simple SHA1 filter.
15+
16+
If `-sha1` is followed by a hex hash, the return code will be zero
917
on match and nonzero otherwise.
1018

11-
Summary
12-
-------
19+
### Summary
20+
1321
This program is a command-line utility to catalog and verify torrent files.
1422
Run with only the -t option, it displays the metadata, name, and size of
1523
each file in the torrent. Run with the -t and -p options, it computes the
@@ -38,44 +46,56 @@ file, and prints the SHA1 hash. If a SHA1 hash is provided on the command line,
3846
it will return 0 if the hashes match or nonzero if they do not. This mode
3947
should agree with the output of "openssl dgst -sha1" or "digest -a sha1"
4048

41-
Examples
42-
--------
43-
torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent
44-
torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download
45-
torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download && echo good
46-
torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download || echo bad
47-
torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download\ubuntu-10.10-desktop-i386.iso
49+
### Examples
50+
51+
```shell
52+
torrentcheck \torrents\ubuntu-10.10-desktop-i386.iso.torrent
53+
torrentcheck \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download
54+
torrentcheck \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download && echo good
55+
torrentcheck \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download || echo bad
56+
torrentcheck \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download\ubuntu-10.10-desktop-i386.iso
4857
torrentcheck -sha1 < \download\ubuntu-10.10-desktop-i386.iso
4958
torrentcheck -sha1 b28bbd742aff85d21b9ad96bb45b67c2d133be99 < \download\ubuntu-10.10-desktop-i386.iso && echo good
59+
```
5060
(These are for Windows; use forward slashes in Unix/Linux)
5161

52-
Automation and scripting
53-
------------------------
62+
### Automation and scripting
63+
5464
Torrentcheck returns 0 in the Unix $? return code or Windows errorlevel
5565
if it successfully verifies a torrent, or nonzero return codes if it fails.
5666

57-
If you have your torrents in \torrents and the downloaded files in \share,
58-
make a "bad" directory under \torrents, cd to \torrents, and run:
67+
If you have your torrents in `\torrents` and the downloaded files in `\share`,
68+
make a "bad" directory under `\torrents`, cd to `\torrents`, and run:
5969

6070
(Windows)
61-
for %i in (*.torrent) do torrentcheck -t "%i" -p \share || move "%i" bad
71+
```shell
72+
for %i in (*.torrent) do torrentcheck "%i" -p \share || move "%i" bad
73+
```
74+
6275
(Linux)
63-
for i in *.torrent; do torrentcheck -t "$i" -p /share || mv "$i" bad ; done
76+
```shell
77+
for i in *.torrent; do torrentcheck "$i" -p /share || mv "$i" bad ; done
78+
```
6479
6580
This will check all the torrents, and move any that are not fully
66-
downloaded and correct into \torrents\bad.
81+
downloaded and correct into `\torrents\bad`.
6782
6883
Run this command to generate a master list file with the contents of all your
6984
torrents. This file can be searched to find a particular file and which torrent
7085
it comes from.
7186
7287
(Windows)
73-
for %i in (*.torrent) do torrentcheck -t "%i" >> masterlist.txt & echo. >> masterlist.txt
88+
```shell
89+
for %i in (*.torrent) do torrentcheck "%i" >> masterlist.txt & echo. >> masterlist.txt
90+
```
91+
7492
(Linux)
75-
for i in *.torrent; do torrentcheck -t "$i" >> masterlist.txt ; echo >> masterlist.txt ; done
93+
```shell
94+
for i in *.torrent; do torrentcheck "$i" >> masterlist.txt ; echo >> masterlist.txt ; done
95+
```
96+
97+
### Detailed description
7698
77-
Detailed description
78-
--------------------
7999
BitTorrent is a file sharing system which uses a metadata file, usually with
80100
the .torrent extension, to identify a data file or group of files. Given the
81101
metadata file, a BitTorrent client can download and share the data files.
@@ -124,9 +144,9 @@ The SHA1 implementation used by torrentcheck was written by David Ireland,
124144
AM Kuchling, and Peter Gutmann. The source code does not contain a copyright
125145
notice, and this file is widely used on the Internet.
126146
127-
Compiling
128-
---------
129-
There is no makefile. The required gcc lines are at the top of the
130-
torrentcheck.c source file. The major catch in compiling is making 64-bit file
147+
### Compiling
148+
149+
There is no makefile. The required `gcc` lines are at the top of the
150+
`torrentcheck.c` source file. The major catch in compiling is making 64-bit file
131151
I/O work. It is tested on Windows, Linux, and Solaris, but you may have to
132152
experiment with compiler options to get 64-bit ftell and fseek working.

torrentcheck.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ void backspaceProgressLine(int *showProgressChars) { // remove the progress line
267267
}
268268

269269

270+
void GetDataString(BYTE* benstr,int benstrOffset,BYTE** stringBegin)
271+
{
272+
*stringBegin = benstr + benstrOffset;
273+
return;
274+
}
275+
276+
270277
int sha1Filter(char* compareHash) {
271278
BYTE inputBuffer[inputBufLen];
272279
SHA_CTX sha1ctx;
@@ -334,6 +341,9 @@ int main(int argc,char* argv[]) {
334341
int numPieces = -1;
335342
int numFiles = 0;
336343
INT64 pieceLen = -1;
344+
BYTE* info = NULL;
345+
int infoEnd = -1;
346+
int infoLen = -1;
337347
BYTE* fileName = NULL;
338348
int fileNameLen = -1;
339349
char* filePath;
@@ -470,6 +480,7 @@ int main(int argc,char* argv[]) {
470480
printf("Unable to parse torrent metadata file %s\n",torrentFile);
471481
return 2;
472482
}
483+
infoEnd = torrentLen;
473484

474485
torrentInfo = beFindInDict(torrent,torrentLen,0,"info");
475486
if (torrentInfo < 0) {
@@ -493,11 +504,6 @@ int main(int argc,char* argv[]) {
493504
}
494505
}
495506

496-
ofs = beFindInDict(torrent,torrentLen,torrentInfo,"private");
497-
if (ofs >= 0) {
498-
ofs = beParseInteger(torrent,torrentLen,ofs,&torrentPrivate);
499-
}
500-
501507
ofs = beFindInDict(torrent,torrentLen,torrentInfo,"length");
502508
if (ofs >= 0) { // single file
503509
ofs = beParseInteger(torrent,torrentLen,ofs,&fileBytesExpected);
@@ -637,6 +643,13 @@ int main(int argc,char* argv[]) {
637643
printf("Unable to read \"pieces\" from torrent\n");
638644
return 2;
639645
}
646+
if (ofs > 0) infoEnd = ofs;
647+
648+
ofs = beFindInDict(torrent,torrentLen,torrentInfo,"private");
649+
if (ofs >= 0) {
650+
ofs = beParseInteger(torrent,torrentLen,ofs,&torrentPrivate);
651+
}
652+
if (ofs > 0) infoEnd = ofs;
640653

641654
numPieces = pieceListLen / SHA1_LEN;
642655
if (numPieces * SHA1_LEN != pieceListLen) {
@@ -653,11 +666,24 @@ int main(int argc,char* argv[]) {
653666
return 2;
654667
}
655668

669+
infoLen = infoEnd - torrentInfo + 1;
670+
GetDataString(torrent,torrentInfo,&info);
671+
656672
printf("Torrent file : %s\n",torrentFile);
657673
printf("Metadata info : %i bytes, %i piece%s, %s bytes per piece%s\n",torrentLen,numPieces,((numPieces==1)?"":"s"),print64(pieceLen,p64Buf1,useCommaDot),((torrentPrivate==1)?", private":""));
658674
printf("Torrent name : ");
659675
fwrite(rootName,rootNameLen,1,stdout);
660676
printf("\n");
677+
// printf("Info length : %d\n", infoLen);
678+
SHAInit(&sha1ctx);
679+
SHAUpdate(&sha1ctx, info, infoLen);
680+
SHAFinal(sha1hash,&sha1ctx);
681+
printf("Torrent hash : %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
682+
(int)sha1hash[0], (int)sha1hash[1], (int)sha1hash[2], (int)sha1hash[3],
683+
(int)sha1hash[4], (int)sha1hash[5], (int)sha1hash[6], (int)sha1hash[7],
684+
(int)sha1hash[8], (int)sha1hash[9], (int)sha1hash[10], (int)sha1hash[11],
685+
(int)sha1hash[12], (int)sha1hash[13], (int)sha1hash[14], (int)sha1hash[15],
686+
(int)sha1hash[16], (int)sha1hash[17], (int)sha1hash[18], (int)sha1hash[19]);
661687

662688
if (multiFileTorrent) {
663689
printf("Content info : %i file%s, %s bytes\n",numFiles,((numFiles==1)?"":"s"),print64(totalBytes,p64Buf1,useCommaDot));

0 commit comments

Comments
 (0)