Formatting and cleanup

This commit is contained in:
Tad 2017-12-14 21:29:43 -05:00
parent 1eb5055b50
commit 6933ca294b
3 changed files with 24 additions and 32 deletions

View file

@ -6,32 +6,28 @@ 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;
import java.util.HashMap;
import java.util.Scanner;
public class Database {
private static Context context = null;
private static TextView log = null;
private static File databasePath = null;
public static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<SignatureDatabase>();
public static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<>();
public static HashMap<String, String> signaturesMD5 = new HashMap<String, String>();
public static HashMap<String, String> signaturesSHA1 = new HashMap<String, String>();
public static HashMap<String, String> signaturesSHA256 = new HashMap<String, String>();
public static HashMap<String, String> signaturesMD5 = new HashMap<>();
public static HashMap<String, String> signaturesSHA1 = new HashMap<>();
public static HashMap<String, String> signaturesSHA256 = new HashMap<>();
public Database(Context context, TextView log) {
this.context = context;
this.log = log;
this.databasePath = new File(context.getFilesDir() + "/signatures/");
this.databasePath.mkdir();
Database.log = log;
databasePath = new File(context.getFilesDir() + "/signatures/");
databasePath.mkdir();
signatureDatabases.add(new SignatureDatabase("https://spotco.us/clamav-main.hdb", "clamav-main.hdb"));
signatureDatabases.add(new SignatureDatabase("https://spotco.us/clamav-main.hsb", "clamav-main.hsb"));
@ -62,7 +58,7 @@ public class Database {
try {
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
while((line = reader.readLine()) != null) {
while ((line = reader.readLine()) != null) {
String[] lineS = line.split(":");
signaturesMD5.put(lineS[0], lineS[2]);
}
@ -74,7 +70,7 @@ public class Database {
try {
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
while((line = reader.readLine()) != null) {
while ((line = reader.readLine()) != null) {
String[] lineS = line.split(":");
if (lineS[0].length() == 32) {
signaturesSHA1.put(lineS[0], lineS[2]);

View file

@ -42,7 +42,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
if (!scanner.isScannerRunning()) {
scanner.startScanner(scanSystem, scanApps, scanInternal, scanExternal);
MalwareScanner.startScanner(scanSystem, scanApps, scanInternal, scanExternal);
} else {
scanner.stopScanner();
}

View file

@ -23,20 +23,20 @@ public class MalwareScanner {
private static TextView log = null;
private static AsyncTask<Boolean, String, String> malwareScannerTask = null;
private static HashMap<String, File> fileHashesMD5 = new HashMap<String, File>();
private static HashMap<String, File> fileHashesSHA1 = new HashMap<String, File>();
private static HashMap<String, File> fileHashesSHA256 = new HashMap<String, File>();
private static HashMap<String, File> fileHashesMD5 = new HashMap<>();
private static HashMap<String, File> fileHashesSHA1 = new HashMap<>();
private static HashMap<String, File> fileHashesSHA256 = new HashMap<>();
public MalwareScanner(Context context, TextView log) {
this.context = context;
this.log = log;
MalwareScanner.context = context;
MalwareScanner.log = log;
}
public static void startScanner(boolean scanSystem, boolean scanApps, boolean scanInternal, boolean scanExternal) {
if(Database.doesDatabaseExist()) {
if (Database.doesDatabaseExist()) {
malwareScannerTask = new MalwareScannerTask().execute(scanSystem, scanApps, scanInternal, scanExternal);
} else {
log.append("No database found... download one first!\n");
log.append("No database found... download one first!\n");
}
}
@ -45,10 +45,7 @@ public class MalwareScanner {
}
public boolean isScannerRunning() {
if (malwareScannerTask == null) {
return false;
}
return malwareScannerTask.getStatus().equals(AsyncTask.Status.PENDING) || malwareScannerTask.getStatus().equals(AsyncTask.Status.RUNNING);
return malwareScannerTask != null && (malwareScannerTask.getStatus().equals(AsyncTask.Status.PENDING) || malwareScannerTask.getStatus().equals(AsyncTask.Status.RUNNING));
}
public static class MalwareScannerTask extends AsyncTask<Boolean, String, String> {
@ -105,7 +102,7 @@ public class MalwareScanner {
if (Database.signaturesMD5.size() > 0) {
for (Map.Entry<String, File> file : fileHashesMD5.entrySet()) {
if(Database.signaturesMD5.containsKey(file.getKey())) {
if (Database.signaturesMD5.containsKey(file.getKey())) {
String result = Database.signaturesMD5.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
@ -117,7 +114,7 @@ public class MalwareScanner {
if (Database.signaturesSHA1.size() > 0) {
for (Map.Entry<String, File> file : fileHashesSHA1.entrySet()) {
if(Database.signaturesSHA1.containsKey(file.getKey())) {
if (Database.signaturesSHA1.containsKey(file.getKey())) {
String result = Database.signaturesSHA1.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
@ -128,9 +125,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()) {
if(Database.signaturesSHA256.containsKey(file.getKey())) {
if (Database.signaturesSHA256.containsKey(file.getKey())) {
String result = Database.signaturesSHA256.get(file.getKey());
publishProgress(file.getValue() + " detected as " + result);
}
@ -179,7 +175,7 @@ public class MalwareScanner {
private static void getFileHashes(File file) {
try {
InputStream fis = new FileInputStream(file);
InputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
int numRead;
@ -190,7 +186,7 @@ public class MalwareScanner {
do {
numRead = fis.read(buffer);
if(numRead > 0) {
if (numRead > 0) {
digestMD5.update(buffer, 0, numRead);
digestSHA1.update(buffer, 0, numRead);
digestSHA256.update(buffer, 0, numRead);
@ -201,7 +197,7 @@ public class MalwareScanner {
fileHashesMD5.put(String.format("%032x", new BigInteger(1, digestMD5.digest())), file);
fileHashesSHA1.put(String.format("%032x", new BigInteger(1, digestSHA1.digest())), file);
fileHashesSHA256.put(String.format( "%064x", new BigInteger(1, digestSHA256.digest())), file);
fileHashesSHA256.put(String.format("%064x", new BigInteger(1, digestSHA256.digest())), file);
} catch (Exception e) {
e.printStackTrace();
}