| 60 | } |
| 61 | |
| 62 | static Bundle lookup(Context context, File file, String apiKey) throws NoSuchAlgorithmException, IOException, JSONException { |
| 63 | Bundle result = new Bundle(); |
| 64 | |
| 65 | Pair<Integer, String> response = call(context, "api/v3/files/" + getHash(file), apiKey); |
| 66 | if (response.first == HttpsURLConnection.HTTP_OK) { |
| 67 | // https://developers.virustotal.com/reference/files |
| 68 | // Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a |
| 69 | JSONObject jroot = new JSONObject(response.second); |
| 70 | JSONObject jdata = jroot.getJSONObject("data"); |
| 71 | JSONObject jattributes = jdata.getJSONObject("attributes"); |
| 72 | |
| 73 | JSONObject jclassification = jattributes.optJSONObject("popular_threat_classification"); |
| 74 | String label = (jclassification == null ? null : jclassification.getString("suggested_threat_label")); |
| 75 | |
| 76 | List<ScanResult> scanResult = new ArrayList<>(); |
| 77 | JSONObject janalysis = jattributes.getJSONObject("last_analysis_results"); |
| 78 | JSONArray jnames = janalysis.names(); |
| 79 | if (jnames != null) { |
| 80 | for (int i = 0; i < jnames.length(); i++) { |
| 81 | String name = jnames.getString(i); |
| 82 | JSONObject jresult = janalysis.getJSONObject(name); |
| 83 | String category = jresult.getString("category"); |
| 84 | scanResult.add(new ScanResult(name, category)); |
| 85 | } |
| 86 | |
| 87 | result.putParcelableArrayList("scans", (ArrayList<? extends Parcelable>) scanResult); |
| 88 | result.putString("label", label); |
| 89 | } |
| 90 | } else if (response.first != HttpsURLConnection.HTTP_NOT_FOUND) |
| 91 | throw new FileNotFoundException(response.second); |
| 92 | |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | static String upload(Context context, File file, String apiKey) throws IOException, JSONException { |
| 97 | // Get upload URL |