MD5 파일 수정 여부 검증

1192 단어 자바
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Password {
	 public static String getMd5ByFile(File file) throws IOException, NoSuchAlgorithmException {
        String value = null;
        FileInputStream in = new FileInputStream(file);
	    MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
	    MessageDigest md5 = MessageDigest.getInstance("MD5");
	    md5.update(byteBuffer);
	    BigInteger bi = new BigInteger(1, md5.digest());
	    value = bi.toString(16);
        if(null != in) {
		     in.close();
	    }
		return value;
	}
	public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
		String path="E:\\solr.log";
		String path2="E:\\solr2.log";
		String v = getMd5ByFile(new File(path));
		String v2 = getMd5ByFile(new File(path2));
		System.out.println("MD5:"+v);
		System.out.println("MD5:"+v2);
	}
}

좋은 웹페이지 즐겨찾기