VirusTotal is a web service that performs url/file scan with some virus scanners. It provides some very simple public API, so that we can automate the file submission and report checking process. There was not a Java class to do this task, so that I decided to code it.
I'm working on the possibility to upload a file and scan it. I let you updated...

>> You can find the whole code here [JVirusTotal]. You can look at the following example for further information.

JVirusTotal vt = new JVirusTotal(your_API_key);
String url = "http://www.x.x";

// submit an URL
vt.submitScanURL(url);

// retrieve an URL scan report
vt.retrieveURLscan(url);

// retrieve a file scan report
vt.retrieveFilescan(getMD5Sum(new URL(url)));

The following class is used to get the MD5 hash of a file, by giving its URL.

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5 {
	/**
	 * it calculates the md5sum
	 * 
	 * @param url file url
	 * @return md5sum
	 */
	public static String getMD5Sum(URL url) {
		MessageDigest digest = null;
				
		try {
			digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
			
		byte[] buffer = new byte[8192];
		int read = 0;
		String output = "";

		InputStream is = null;
		
		try {
			is = url.openStream();

			while( (read = is.read(buffer)) > 0) {
				digest.update(buffer, 0, read);
			}		
			byte[] md5sum = digest.digest();
			BigInteger bigInt = new BigInteger(1, md5sum);
			output = bigInt.toString(16);
		}
		catch(IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
			
		return output;
	}
	
	public static void main (String[] s) throws MalformedURLException{
		System.out.println(getMD5Sum(new URL("http://www.x.x")));
	}
}

8 Responses to Using VirusTotal API with Java

  1. 83 abhishekkr 2010-10-08 11:39 am

    nice useful attempt :)

  2. 84 jaikappa 2010-10-08 12:58 pm

    Good ;)

  3. 111 Sharl 2010-12-11 6:15 am

    Great doc! it will be very helpful for more & more java's VirusTotal extensions. thx~

  4. 163 sheryl 2011-01-04 10:45 am

    can u send the java code used to upload the file to virus total

  5. 164 sneak 2011-01-04 11:33 am

    @sheryl
    I'm quite busy during this period.. I'll upload it as finished! :)
    I let you tuned.

  6. 166 sheryl 2011-01-06 9:48 am

    how much time approximately will it take for u to send it.iam in an urgent need of it.thanx for ur reply

  7. 167 yankee 2011-01-06 11:21 am

    great doc

  8. 1447 deredes.net 2011-11-12 7:31 pm

    Great APi!!




Main Pages

Twitter