简体   繁体   中英

Current way of generating MD5 hash (Java 8/9)

Working on an old java project that generates MD5 hash strings got me wondering about the current state of things.

Is there a good way to create such hash using current technologies? Maybe some new libraries for Java 8/9?

The way I generated MD5 hash before was:

 import java.security.*;
 import java.math.*;

 public class MD5 {
    public static void main(String args[]) throws Exception{
       String s="This is a test";
       MessageDigest m=MessageDigest.getInstance("MD5");
       m.update(s.getBytes(),0,s.length());
       System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
   }
}

Thanks.

I recently build my Malware Analysis project in which I generate MD5 hash for all files in directory and sub directory.

I used Apache Commons Codec .

Here is the related part of code:

for (File file : fList) {
    if (file.isFile()) {
        try {
                fin = new FileInputStream(file);
                String digest = DigestUtils.md5Hex(fin); //used to get MD5 
                String path = file.getAbsolutePath(); //return full path
                hash.put(path, digest); //put path and MD5 in map
                fin.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    } else if (file.isDirectory()) {
        listFiles(file.getAbsolutePath()); // recursivily call
    }
}

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM