Skip to content

Commit 883b72a

Browse files
committed
Merge branch 'pr-1850'
2 parents edd4484 + 4d028d1 commit 883b72a

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

iped-engine/src/main/java/iped/engine/core/Manager.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.charset.StandardCharsets;
2626
import java.nio.file.Files;
2727
import java.nio.file.StandardOpenOption;
28+
import java.security.Security;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.concurrent.atomic.AtomicBoolean;
@@ -47,6 +48,7 @@
4748
import org.apache.lucene.search.MatchAllDocsQuery;
4849
import org.apache.lucene.search.TermQuery;
4950
import org.apache.lucene.store.Directory;
51+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
5052

5153
import iped.data.ICaseData;
5254
import iped.data.IItem;
@@ -154,7 +156,13 @@ public class Manager {
154156
private final AtomicBoolean initSleuthkitServers = new AtomicBoolean(false);
155157

156158
private static final String appWinExeFileName = "IPED-SearchApp.exe";
157-
159+
160+
static {
161+
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
162+
Security.addProvider(new BouncyCastleProvider());
163+
}
164+
}
165+
158166
public static Manager getInstance() {
159167
return instance;
160168
}

iped-engine/src/main/java/iped/engine/task/HashTask.java

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import java.util.HashMap;
2727
import java.util.LinkedHashMap;
2828
import java.util.List;
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.ExecutorService;
31+
import java.util.concurrent.Executors;
32+
import java.util.concurrent.atomic.AtomicReference;
2933

30-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3134
import org.slf4j.Logger;
3235
import org.slf4j.LoggerFactory;
3336

@@ -36,7 +39,6 @@
3639
import iped.engine.config.ConfigurationManager;
3740
import iped.engine.config.HashTaskConfig;
3841
import iped.parsers.whatsapp.WhatsAppParser;
39-
import iped.utils.IOUtil;
4042

4143
/**
4244
* Classe para calcular e manipular hashes.
@@ -45,6 +47,10 @@ public class HashTask extends AbstractTask {
4547

4648
private static Logger LOGGER = LoggerFactory.getLogger(HashTask.class);
4749

50+
private static final int HASH_BUFFER_LEN = 1024 * 1024;
51+
52+
private static final ExecutorService executorService = Executors.newCachedThreadPool();
53+
4854
public enum HASH {
4955
MD5("md5"), //$NON-NLS-1$
5056
SHA1("sha-1"), //$NON-NLS-1$
@@ -87,7 +93,7 @@ public void init(ConfigurationManager configurationManager) throws Exception {
8793
if (!algorithm.equalsIgnoreCase(HASH.EDONKEY.toString())) {
8894
digest = MessageDigest.getInstance(algorithm.toUpperCase());
8995
} else {
90-
digest = MessageDigest.getInstance("MD4", new BouncyCastleProvider()); //$NON-NLS-1$
96+
digest = MessageDigest.getInstance("MD4"); //$NON-NLS-1$
9197
}
9298
digestMap.put(algorithm, digest);
9399
if (HASH.SHA256.toString().equals(algorithm)) {
@@ -99,8 +105,9 @@ public void init(ConfigurationManager configurationManager) throws Exception {
99105

100106
@Override
101107
public void finish() throws Exception {
102-
// TODO Auto-generated method stub
103-
108+
if (!executorService.isShutdown()) {
109+
executorService.shutdown();
110+
}
104111
}
105112

106113
public void process(IItem evidence) {
@@ -119,19 +126,54 @@ public void process(IItem evidence) {
119126
return;
120127
}
121128

122-
InputStream in = null;
123-
try {
124-
in = evidence.getBufferedInputStream();
125-
byte[] buf = new byte[1024 * 1024];
129+
try (InputStream in = evidence.getBufferedInputStream()) {
130+
131+
byte[] readBuf = new byte[HASH_BUFFER_LEN];
132+
byte[] hashBuf = new byte[HASH_BUFFER_LEN];
133+
byte[] tempBuf = null;
126134
int len;
127-
while ((len = in.read(buf)) >= 0 && !Thread.currentThread().isInterrupted()) {
135+
136+
AtomicReference<CountDownLatch> countDown = new AtomicReference<>(null);
137+
AtomicReference<Exception> ex = new AtomicReference<Exception>(null);
138+
139+
while ((len = in.read(readBuf)) >= 0 && !Thread.currentThread().isInterrupted()) {
140+
141+
if (countDown.get() != null) {
142+
countDown.get().await();
143+
}
144+
145+
countDown.set(new CountDownLatch(digestMap.size()));
146+
147+
// swap hashBuf <-> readBuf
148+
tempBuf = hashBuf;
149+
hashBuf = readBuf;
150+
readBuf = tempBuf;
151+
152+
final int currLen = len;
153+
final byte[] currHashBuf = hashBuf;
128154
for (String algo : digestMap.keySet()) {
129-
if (!algo.equals(HASH.EDONKEY.toString())) {
130-
digestMap.get(algo).update(buf, 0, len);
131-
} else {
132-
updateEd2k(buf, len);
133-
}
155+
executorService.execute(() -> {
156+
try {
157+
if (!algo.equals(HASH.EDONKEY.toString())) {
158+
digestMap.get(algo).update(currHashBuf, 0, currLen);
159+
} else {
160+
updateEd2k(currHashBuf, currLen);
161+
}
162+
} catch (Exception e) {
163+
ex.set(e);
164+
} finally {
165+
countDown.get().countDown();
166+
}
167+
});
134168
}
169+
170+
if (ex.get() != null) {
171+
throw ex.get();
172+
}
173+
}
174+
175+
if (countDown.get() != null) {
176+
countDown.get().await();
135177
}
136178

137179
boolean defaultHash = true;
@@ -161,8 +203,6 @@ public void process(IItem evidence) {
161203
e.toString());
162204
// e.printStackTrace();
163205

164-
} finally {
165-
IOUtil.closeQuietly(in);
166206
}
167207

168208
}

iped-engine/src/main/java/iped/engine/task/regex/validator/cripto/EthereumAddressValidatorService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.List;
88

99
import org.bouncycastle.jcajce.provider.digest.Keccak;
10-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1110

1211
import iped.engine.task.regex.BasicAbstractRegexValidatorService;
1312

@@ -23,7 +22,6 @@ public class EthereumAddressValidatorService extends BasicAbstractRegexValidator
2322
private static final int[] MASKS = { 128, 8 };
2423

2524
static {
26-
Security.addProvider(new BouncyCastleProvider());
2725
digest = new Keccak.Digest256();
2826
}
2927

0 commit comments

Comments
 (0)