Hypatia/app/src/main/java/us/spotco/malwarescanner/Database.java
2017-12-14 14:56:54 -05:00

151 lines
5.7 KiB
Java

package us.spotco.malwarescanner;
import android.content.Context;
import android.os.AsyncTask;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 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 Database(Context context, TextView log) {
this.context = context;
this.log = log;
this.databasePath = new File(context.getFilesDir() + "/signatures/");
this.databasePath.mkdir();
}
public static boolean doesDatabaseExist() {
return databasePath.listFiles().length > 0;
}
public static void updateDatabase(ArrayList<SignatureDatabase> signatureDatabases) {
for(SignatureDatabase signatureDatabase : signatureDatabases) {
new Downloader().execute(signatureDatabase.getUrl(), databasePath + "/" + signatureDatabase.getName());
}
}
public static void loadDatabase(ArrayList<SignatureDatabase> signatureDatabases) {
for(SignatureDatabase database : signatureDatabases) {
File databaseLocation = new File(databasePath + "/" + database.getName());
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]);
}
scanner.close();
} catch (FileNotFoundException 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]);
} else {
signaturesSHA256.put(line[0], line[2]);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
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
protected String doInBackground(String... strings) {
String url = strings[0];
File out = new File(strings[1]);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(45000);
connection.setReadTimeout(45000);
connection.addRequestProperty("User-Agent", "Veritas Database Updater");
if (out.exists()) {
connection.setIfModifiedSince(out.lastModified());
}
connection.connect();
int res = connection.getResponseCode();
if (res != 304) {
if(res == 200) {
if (out.exists()) {
out.delete();
}
FileOutputStream fileOutputStream = new FileOutputStream(out);
final byte data[] = new byte[1024];
int count;
while ((count = connection.getInputStream().read(data, 0, 1024)) != -1) {
fileOutputStream.write(data, 0, count);
}
fileOutputStream.close();
publishProgress("Successfully downloaded " + url + "\n");
} else {
publishProgress("File not downloaded " + res + "\n");
}
} else {
publishProgress("File not changed " + url + "\n");
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
publishProgress("Failed to download file from " + url + "\n");
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
log.append(progress[0] + "\n");
}
@Override
protected void onPostExecute(String result) {
}
}
}