Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package test;
- /**
- *
- * @author leo
- */
- import java.io.FileInputStream;
- import java.security.MessageDigest;
- public class FileToMD5 {
- @SuppressWarnings("empty-statement")
- public static void main(String[] args) throws Exception {
- String str = ToMD5("D:/Coding/HashCalc.exe");
- System.out.println(str);
- }
- private static String ToMD5(String FilePath) throws Exception {
- MessageDigest md = MessageDigest.getInstance("MD5");
- FileInputStream fis = new FileInputStream(FilePath);
- byte[] dataBytes = new byte[1024];
- int nread = 0;
- while ((nread = fis.read(dataBytes)) != -1) {
- md.update(dataBytes, 0, nread);
- };
- byte[] mdbytes = md.digest();
- //convert the byte to hex
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < mdbytes.length; i++) {
- sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment