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; private static HashMap signaturesMD5 = new HashMap(); private static HashMap signaturesSHA256 = new HashMap(); public Database(Context context, TextView log) { this.context = context; this.log = log; this.databasePath = new File(context.getFilesDir() + "signatures/"); } public static boolean doesDatabaseExist() { return databasePath.listFiles().length > 0; } public static void updateDatabase(ArrayList signatureDatabases) { for(SignatureDatabase signatureDatabase : signatureDatabases) { new Downloader().execute(signatureDatabase.getUrl(), context.getFilesDir() + signatureDatabase.getName()); } } public static void loadDatabase(ArrayList signatureDatabases) { for(SignatureDatabase database : signatureDatabases) { File databaseLocation = new File(databasePath + database.getName()); if(!databaseLocation.exists()) { log.append("Database " + database.getName() + " doesn't exist!\n"); } else { 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(":"); 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); } } else if(hash.length() == 64) { if (signaturesSHA256.containsKey(hash)) { return signaturesSHA256.get(hash); } } return null; } public static class Downloader extends AsyncTask { @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) { } } }