More performance improvements

- Switch from Scanner to BufferedReader for loading database
- Don't check the length of hashes for checking them
This commit is contained in:
Tad 2017-12-14 21:21:29 -05:00
parent 8199d43c6d
commit 1eb5055b50
3 changed files with 24 additions and 42 deletions

View file

@ -21,5 +21,4 @@ dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.google.guava:guava:23.5-android'
}

View file

@ -4,9 +4,11 @@ import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
@ -58,28 +60,30 @@ public class Database {
if (databaseLocation.exists()) {
if (database.getName().contains(".hdb")) {//.hdb format: md5, size, name
try {
Scanner scanner = new Scanner(databaseLocation);
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(":");
signaturesMD5.put(line[0], line[2]);
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
while((line = reader.readLine()) != null) {
String[] lineS = line.split(":");
signaturesMD5.put(lineS[0], lineS[2]);
}
scanner.close();
} catch (FileNotFoundException e) {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (database.getName().contains(".hsb")) {//.hsb format: sha256, size, name
try {
Scanner scanner = new Scanner(databaseLocation);
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(":");
if (line[0].length() == 32) {
signaturesSHA1.put(line[0], line[2]);
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
while((line = reader.readLine()) != null) {
String[] lineS = line.split(":");
if (lineS[0].length() == 32) {
signaturesSHA1.put(lineS[0], lineS[2]);
} else {
signaturesSHA256.put(line[0], line[2]);
signaturesSHA256.put(lineS[0], lineS[2]);
}
}
scanner.close();
} catch (FileNotFoundException e) {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@ -88,22 +92,6 @@ public class Database {
System.gc();
}
public static String checkInDatabase(String hash) {
if (hash.length() == 32) {
if (signaturesMD5.containsKey(hash)) {
return signaturesMD5.get(hash);
}
if (signaturesSHA1.containsKey(hash)) {
return signaturesSHA1.get(hash);
}
} else if (hash.length() == 64) {
if (signaturesSHA256.containsKey(hash)) {
return signaturesSHA256.get(hash);
}
}
return null;
}
public static class Downloader extends AsyncTask<String, String, String> {
@Override

View file

@ -7,13 +7,8 @@ import android.os.AsyncTask;
import android.os.Environment;
import android.widget.TextView;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
@ -110,8 +105,8 @@ public class MalwareScanner {
if (Database.signaturesMD5.size() > 0) {
for (Map.Entry<String, File> file : fileHashesMD5.entrySet()) {
String result = Database.checkInDatabase(file.getKey());
if (result != null) {
if(Database.signaturesMD5.containsKey(file.getKey())) {
String result = Database.signaturesMD5.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
}
@ -122,8 +117,8 @@ public class MalwareScanner {
if (Database.signaturesSHA1.size() > 0) {
for (Map.Entry<String, File> file : fileHashesSHA1.entrySet()) {
String result = Database.checkInDatabase(file.getKey());
if (result != null) {
if(Database.signaturesSHA1.containsKey(file.getKey())) {
String result = Database.signaturesSHA1.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
}
@ -135,8 +130,8 @@ public class MalwareScanner {
if (Database.signaturesSHA256.size() > 0) {
publishProgress("Calculated SHA-256 hashes for all files");
for (Map.Entry<String, File> file : fileHashesSHA256.entrySet()) {
String result = Database.checkInDatabase(file.getKey());
if (result != null) {
if(Database.signaturesSHA256.containsKey(file.getKey())) {
String result = Database.signaturesSHA256.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
}