Functions to convert String and File to MD5 in Java 7?

-1

Does anyone know proven and functional functions for what I need?

I have a list of Files stored in a variable and when it is on it to know if i is a folder or file:

                    if (listFile[i].isDirectory()) {
                    //md5 = (JSONObject)parser.parse(getMD5(listFile[i].getAbsolutePath()));
                    //obj.put("md5", md5);
                    obj.put("type", "folder");
                    obj.put("path", listFile[i].getAbsolutePath());
                    JSONArray contentSon = new JSONArray();
                    obj.put("Content", contentSon);
                    drawJsonTree(listFile[i], contentSon);
                    numDir++;
                } else if (listFile[i].isFile()) {
                    obj.put("type", "file");
                    obj.put("path", listFile[i].getAbsolutePath());
                    numFiles++;
                }

I need to encrypt anything to MD5, either a File or the path to a directory ...

I have this function that I take from Google for the topic of routes to directories:

public static String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);

            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

But I'm finding it hard to find one for the Files ....

Example of if it comes out of the "else if" (I need you to encrypt that file)

  

"type": "file", "name":   "JV.Coldview.DTFtoPDF.Service.AMCOX.Compatible.jar", "md5":   "........."

Both functions have to be useful for JAVA 7 because that's what the machine has!

Thank you very much!

    
asked by Nacho Zve De La Torre 24.09.2018 в 15:44
source

1 answer

0

I leave what I used in case someone serves:

public static String md5OfString(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);

            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    } // end de getMD5()

    public static String md5OfFile(File file) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            FileInputStream fs = new FileInputStream(file);
            BufferedInputStream bs = new BufferedInputStream(fs);
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = bs.read(buffer, 0, buffer.length)) != -1) {
                md.update(buffer, 0, bytesRead);
            }
            byte[] digest = md.digest();

            StringBuilder sb = new StringBuilder();
            for (byte bite : digest) {
                sb.append(String.format("%02x", bite & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException | IOException ex) {
            Logger.getLogger(ComponentCompare.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
answered by 24.09.2018 / 16:35
source