Skip to content

Commit 783e347

Browse files
committed
Implemented info hash extraction
1 parent 3f14f71 commit 783e347

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class TorrentFileParser {
1010
private final byte[] bytes;
1111
private int iterator = 0;
1212
private TorrentBuilder torrentBuilder = null;
13+
private int infoDictStartIndex = -1;
1314

1415
public TorrentFileParser(byte[] bytes) {
1516
this.bytes = bytes;
@@ -63,6 +64,9 @@ private Map<String, Object> parseDict() throws MalformedTorrentFileException {
6364
Object parseValue = parse();
6465
if (key == null) {
6566
key = String.valueOf(parseValue);
67+
if (key.equals("info")) {
68+
infoDictStartIndex = iterator;
69+
}
6670
} else {
6771
map.put(key, parseValue);
6872
if (torrentBuilder != null) {
@@ -71,6 +75,9 @@ private Map<String, Object> parseDict() throws MalformedTorrentFileException {
7175
key = null;
7276
}
7377
}
78+
if (infoDictStartIndex != -1 && torrentBuilder != null) {
79+
torrentBuilder.setInfoHash(Arrays.copyOfRange(bytes, infoDictStartIndex, iterator));
80+
}
7481
iterator++; // Skipping closing e
7582
return map;
7683
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Torrent {
1313
private Long totalLength = 0L;
1414
private String pieces;
1515
private final List<TorrentFile> files = new ArrayList<>();
16+
private String infoHash;
1617

1718
// Optional fields
1819
private List<List<String>> announceList = null;
@@ -139,6 +140,14 @@ public void setAzureusProperties(Map<String, Object> azureusProperties) {
139140
this.azureusProperties = azureusProperties;
140141
}
141142

143+
public String getInfoHash() {
144+
return infoHash;
145+
}
146+
147+
public void setInfoHash(String infoHash) {
148+
this.infoHash = infoHash;
149+
}
150+
142151
@Override
143152
public String toString() {
144153
return "Torrent{" +
@@ -149,6 +158,7 @@ public String toString() {
149158
", isPrivate=" + isPrivate +
150159
", totalLength=" + totalLength +
151160
", files=" + files +
161+
", infoHash='" + infoHash + '\'' +
152162
", announceList=" + announceList +
153163
", creator='" + creator + '\'' +
154164
", creationDate=" + creationDate +

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

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

3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.util.HexFormat;
36
import java.util.List;
47
import java.util.Map;
58

@@ -14,30 +17,42 @@ public void processKeyValue(String key, Object value) {
1417
case "announce-list" -> torrent.setAnnounceList((List<List<String>>) value);
1518
case "azureus_properties" -> torrent.setAzureusProperties((Map<String, Object>) value);
1619
case "created by" -> torrent.setCreator(String.valueOf(value));
17-
case "creation date" -> torrent.setCreationDate((Long) value);
20+
case "creation date" -> torrent.setCreationDate((long) value);
1821
case "encoding" -> torrent.setEncoding(String.valueOf(value));
1922
case "files" -> torrent.setSingleFile(false);
2023
case "length" -> {
21-
lastLength = (Long) value;
24+
lastLength = (long) value;
2225
torrent.setTotalLength(torrent.getTotalLength() + lastLength);
2326
}
2427
case "path" -> torrent.getFiles().add(new TorrentFile(lastLength, formatFilePath(String.valueOf(value))));
25-
case "piece length" -> torrent.setPieceLength((Long) value);
28+
case "piece length" -> torrent.setPieceLength((long) value);
2629
case "source" -> torrent.setSource(String.valueOf(value));
2730
case "pieces" -> torrent.setPieces(String.valueOf(value));
2831
case "comment" -> torrent.setComment(String.valueOf(value));
29-
case "private" -> {
30-
boolean isPrivate = (Long) value == 1;
31-
torrent.setPrivate(isPrivate);
32-
}
32+
case "private" -> torrent.setPrivate((long) value == 1);
33+
}
34+
}
35+
36+
public void setInfoHash(byte[] infoBytes) {
37+
try {
38+
torrent.setInfoHash(getSHAsum(infoBytes));
39+
} catch (NoSuchAlgorithmException e) {
40+
throw new RuntimeException(e);
3341
}
3442
}
3543

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);
52+
}
53+
3654
private String formatFilePath(String path) {
37-
return path
38-
.replace("[", "")
39-
.replace("]", "")
40-
.replace(", ", "/");
55+
return path.replace("[", "").replace("]", "").replace(", ", "/");
4156
}
4257

4358
public Torrent getTorrent() {

0 commit comments

Comments
 (0)