26
26
import java .util .HashMap ;
27
27
import java .util .LinkedHashMap ;
28
28
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 ;
29
33
30
- import org .bouncycastle .jce .provider .BouncyCastleProvider ;
31
34
import org .slf4j .Logger ;
32
35
import org .slf4j .LoggerFactory ;
33
36
36
39
import iped .engine .config .ConfigurationManager ;
37
40
import iped .engine .config .HashTaskConfig ;
38
41
import iped .parsers .whatsapp .WhatsAppParser ;
39
- import iped .utils .IOUtil ;
40
42
41
43
/**
42
44
* Classe para calcular e manipular hashes.
@@ -45,6 +47,10 @@ public class HashTask extends AbstractTask {
45
47
46
48
private static Logger LOGGER = LoggerFactory .getLogger (HashTask .class );
47
49
50
+ private static final int HASH_BUFFER_LEN = 1024 * 1024 ;
51
+
52
+ private static final ExecutorService executorService = Executors .newCachedThreadPool ();
53
+
48
54
public enum HASH {
49
55
MD5 ("md5" ), //$NON-NLS-1$
50
56
SHA1 ("sha-1" ), //$NON-NLS-1$
@@ -87,7 +93,7 @@ public void init(ConfigurationManager configurationManager) throws Exception {
87
93
if (!algorithm .equalsIgnoreCase (HASH .EDONKEY .toString ())) {
88
94
digest = MessageDigest .getInstance (algorithm .toUpperCase ());
89
95
} else {
90
- digest = MessageDigest .getInstance ("MD4" , new BouncyCastleProvider () ); //$NON-NLS-1$
96
+ digest = MessageDigest .getInstance ("MD4" ); //$NON-NLS-1$
91
97
}
92
98
digestMap .put (algorithm , digest );
93
99
if (HASH .SHA256 .toString ().equals (algorithm )) {
@@ -99,8 +105,9 @@ public void init(ConfigurationManager configurationManager) throws Exception {
99
105
100
106
@ Override
101
107
public void finish () throws Exception {
102
- // TODO Auto-generated method stub
103
-
108
+ if (!executorService .isShutdown ()) {
109
+ executorService .shutdown ();
110
+ }
104
111
}
105
112
106
113
public void process (IItem evidence ) {
@@ -119,19 +126,54 @@ public void process(IItem evidence) {
119
126
return ;
120
127
}
121
128
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 ;
126
134
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 ;
128
154
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
+ });
134
168
}
169
+
170
+ if (ex .get () != null ) {
171
+ throw ex .get ();
172
+ }
173
+ }
174
+
175
+ if (countDown .get () != null ) {
176
+ countDown .get ().await ();
135
177
}
136
178
137
179
boolean defaultHash = true ;
@@ -161,8 +203,6 @@ public void process(IItem evidence) {
161
203
e .toString ());
162
204
// e.printStackTrace();
163
205
164
- } finally {
165
- IOUtil .closeQuietly (in );
166
206
}
167
207
168
208
}
0 commit comments