Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import java.math.BigInteger;

import java.security.MessageDigest;

public class Main {
    public static String md5Calc(File f) {
        int i;

        byte[] buffer = new byte[1024];
        int read = 0;
        String md5hash;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            InputStream is = new FileInputStream(f);
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            md5hash = bigInt.toString(16);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        if (md5hash.length() != 33) {
            String tmp = "";
            for (i = 1; i < (33 - md5hash.length()); i++) {
                tmp = tmp.concat("0");
            }
            md5hash = tmp.concat(md5hash);
        }

        return md5hash;
    }
}