Reputation: 313
I have a very simple code, which is designed to avoid OutOfMemory exceptions. To this reason, the file is streamed and chunks are created, each chunk is hashed, and the final hash is generated from these (this called a Hash tree: https://en.wikipedia.org/wiki/Hash_list).
My questions are the following:
The code sample is using a 5GB test file downloaded from: https://testfiledownload.com/
My code is the following:
package main;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StopWatch;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) throws IOException {
// Sample test file downloaded from: https://testfiledownload.com/
StopWatch sw = new StopWatch();
sw.start();
String h_text = encryptor(iparser("C:\\Users\\Thend\\Desktop\\test_5gb\\5gb.test"));
sw.stop();
System.out.println(" Hash: "+h_text+" | Execution time: "+String.format("%.5f", sw.getTotalTimeMillis() / 1000.0f)+"sec");
}
public static List<byte[]> iparser (String file) throws IOException {
List<byte[]> temp = new ArrayList<>();
if (Files.size(Path.of(file)) > 104857600) { // 100 MB = 104857600 Bytes (in binary)
// Add chunk by chunk
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[10485760]; // 10 MB chunk
int len;
while ((len = fis.read(buffer)) > 0) {
temp.add(buffer);
}
return temp;
}
} else {
// Add whole
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[(int) file.length()]; // Add whole
int len;
while ((len = fis.read(buffer)) > 0) {
temp.add(buffer);
}
return temp;
}
}
}
public static String encryptor(List<byte[]> list) {
BCryptPasswordEncoder bcpe = new BCryptPasswordEncoder();
ArrayList<String> temp = new ArrayList<>();
if (list.size() > 1) {
// If there is more than one element in the list
list.forEach((n) -> {
//String tohash = new String(n, StandardCharsets.UTF_8);
String tohash = new String(n);
String hashedByteArray = bcpe.encode(tohash);
temp.add((hashedByteArray.split(Pattern.quote("$"))[3]).substring(22));
});
return bcpe.encode(String.join("", temp));
} else {
// If there is only one element in the list
return bcpe.encode(String.join("", temp));
}
}
}
The console output:
Hash: $2a$10$60BSOrudT4BT3RC4dqcooupGd6fmg0/LU0RLGhBTSLvbZgypGuyBq | Execution time: 246,88400sec
Process finished with exit code 0
Upvotes: 0
Views: 365