lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.luck.picture.lib.tools;
 
import android.net.Uri;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
 
/**
 * @author:luck
 * @date:2019-12-30 15:08
 * @describe:Digest
 */
public class Digest {
    /**
     * 获取文件MD5值
     *
     * @param file
     * @return
     */
    public static String computeMD5(File file) {
        return compute(file, "MD5");
    }
 
    /**
     * 获取文件MD5值
     *
     * @param inputStream
     * @return
     */
    public static String computeToQMD5(FileInputStream inputStream) {
        return computeToQ(inputStream, "MD5");
    }
 
    private static String computeToQ(FileInputStream fis, String type) {
 
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(type);
            byte[] buffer = new byte[1024 * 1024];
            for (int bytesRead; (bytesRead = fis.read(buffer)) != -1; ) {
                messageDigest.update(buffer, 0, bytesRead);
            }
 
            return bytesToHex(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    /**
     * 获取文件MD5值
     *
     * @param inputStream
     * @return
     */
    public static String computeToQMD5(InputStream inputStream) {
        return computeToQ(inputStream, "MD5");
    }
 
    private static String computeToQ(InputStream fis, String type) {
 
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(type);
            byte[] buffer = new byte[1024 * 1024];
            for (int bytesRead; (bytesRead = fis.read(buffer)) != -1; ) {
                messageDigest.update(buffer, 0, bytesRead);
            }
 
            return bytesToHex(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    private static String compute(File file, String type) {
        FileInputStream fis = null;
 
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(type);
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024 * 1024];
 
            for (int bytesRead = 0; (bytesRead = fis.read(buffer)) != -1; ) {
                messageDigest.update(buffer, 0, bytesRead);
            }
 
            return bytesToHex(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
 
        for (byte b : bytes) {
            sb.append(byteToHex(b));
        }
 
        return sb.toString();
    }
 
    private static String byteToHex(byte b) {
        String hex = Integer.toHexString(0xFF & b | 0x00);
        return b >= 0 && b <= 15 ? '0' + hex : hex;
    }
}