Skip to content

Commit ffa1e9e

Browse files
committed
Fixed info hash calculation, changed readme, added option to disable info hash calculation
1 parent 41e22d7 commit ffa1e9e

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A simple java library that allows you to read the contents of a torrent file.
1717
String pieces;
1818
boolean isSingleFile;
1919
boolean isPrivate;
20-
String infoHash;
20+
String infoHash; // null if computeInfoHash is set to false
2121

2222
// Optional fields
2323
List<List<String>> announceList;

src/main/java/com/robothaver/torrentfileparser/TorrentFileParser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
public class TorrentFileParser {
1010
private final byte[] bytes;
11+
private final boolean computeInfoHash;
1112
private int iterator = 0;
1213
private TorrentBuilder torrentBuilder = null;
1314
private int infoDictStartIndex = -1;
1415

15-
public TorrentFileParser(byte[] bytes) {
16+
public TorrentFileParser(byte[] bytes, boolean computeInfoHash) {
1617
this.bytes = bytes;
18+
this.computeInfoHash = computeInfoHash;
1719
}
1820

1921
private Object parse() throws MalformedTorrentFileException {
@@ -69,15 +71,15 @@ private Map<String, Object> parseDict() throws MalformedTorrentFileException {
6971
}
7072
} else {
7173
map.put(key, parseValue);
74+
if (key.equals("info") && computeInfoHash && torrentBuilder != null) {
75+
torrentBuilder.setInfoHash(Arrays.copyOfRange(bytes, infoDictStartIndex, iterator));
76+
}
7277
if (torrentBuilder != null) {
7378
torrentBuilder.processKeyValue(key, parseValue);
7479
}
7580
key = null;
7681
}
7782
}
78-
if (infoDictStartIndex != -1 && torrentBuilder != null) {
79-
torrentBuilder.setInfoHash(Arrays.copyOfRange(bytes, infoDictStartIndex, iterator));
80-
}
8183
iterator++; // Skipping closing e
8284
return map;
8385
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.robothaver.torrentfileparser.domain;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.util.HexFormat;
6+
7+
public class InfoHasCompute {
8+
public static String getInfoHash(byte[] infoBytes) {
9+
try {
10+
return getSHAsum(infoBytes);
11+
} catch (NoSuchAlgorithmException e) {
12+
throw new RuntimeException(e);
13+
}
14+
}
15+
16+
private static String getSHAsum(byte[] input) throws NoSuchAlgorithmException {
17+
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
18+
return byteArrayToHex(messageDigest.digest(input));
19+
}
20+
21+
private static String byteArrayToHex(byte[] bytes) {
22+
HexFormat hex = HexFormat.of();
23+
return hex.formatHex(bytes);
24+
}
25+
}

src/main/java/com/robothaver/torrentfileparser/domain/TorrentBuilder.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.robothaver.torrentfileparser.domain;
22

3-
import java.security.MessageDigest;
4-
import java.security.NoSuchAlgorithmException;
5-
import java.util.HexFormat;
63
import java.util.List;
74
import java.util.Map;
85

@@ -34,21 +31,7 @@ public void processKeyValue(String key, Object value) {
3431
}
3532

3633
public void setInfoHash(byte[] infoBytes) {
37-
try {
38-
torrent.setInfoHash(getSHAsum(infoBytes));
39-
} catch (NoSuchAlgorithmException e) {
40-
throw new RuntimeException(e);
41-
}
42-
}
43-
44-
private String getSHAsum(byte[] input) throws NoSuchAlgorithmException {
45-
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
46-
return byteArrayToHex(messageDigest.digest(input));
47-
}
48-
49-
private String byteArrayToHex(byte[] bytes) {
50-
HexFormat hex = HexFormat.of();
51-
return hex.formatHex(bytes);
34+
torrent.setInfoHash(InfoHasCompute.getInfoHash(infoBytes));
5235
}
5336

5437
private String formatFilePath(String path) {

0 commit comments

Comments
 (0)