wjc
2023-06-27 f8656588595af6cb716341b5daacba26e350a872
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.hdl.photovoltaic.utils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public class HDLMD5Utils {
    /**
     * MD5转字符串
     */
    public static String encodeMD5(String s) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(s.getBytes(StandardCharsets.UTF_8));
            byte[] messageDigest = digest.digest();
            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    private static String toHexString(byte[] keyData) {
        if (keyData == null) {
            return null;
        }
        int expectedStringLen = keyData.length * 2;
        StringBuilder sb = new StringBuilder(expectedStringLen);
        for (byte keyDatum : keyData) {
            String hexStr = Integer.toString(keyDatum & 0x00FF, 16);
            if (hexStr.length() == 1) {
                hexStr = "0" + hexStr;
            }
            sb.append(hexStr);
        }
        return sb.toString();
    }
 
    /**
     * MD5转字符串
     * 额外值
     */
    public static String encodeMD52(String s) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = s.getBytes(StandardCharsets.UTF_8);
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(btInput);
            byte[] md = digest.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * MD5文件转字符串
     */
    public static String encodeMD5(File f) {
        if (!f.isFile()) {
            return null;
        }
        MessageDigest digest;
        byte[] buffer = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            FileInputStream in = new FileInputStream(f);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }
 
    /**
     * MD5文件转字符串
     */
    public static String encodeMD5(byte[] f) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.update(f);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }
 
 
    public final static String md5(String plainText) {
 
// 返回字符串
 
        String md5Str = null;
 
        try {
 
// 操作字符串
 
            StringBuffer buf = new StringBuffer();
 
            MessageDigest md =
 
                    MessageDigest.getInstance("MD5");
 
// 添加要进行计算摘要的信息,使用 plainText 的 byte
 
 
            md.update(plainText.getBytes());
 
// 计算出摘要,完成哈希计算。
 
            byte b[] = md.digest();
 
            int i;
 
            for (int offset = 0; offset < b.length; offset++) {
 
                i = b[offset];
 
                if (i < 0) {
 
                    i += 256;
 
                }
 
                if (i < 16) {
 
                    buf.append("0");
 
                }
 
// 将整型 十进制 i
 
 
                buf.append(Integer.toHexString(i));
 
            }
 
// 32位的加密
 
            md5Str = buf.toString();
 
// 16位的加密
 
// md5Str =  buf.toString().md5Strstring(8,24);
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
 
        return md5Str;
 
    }
}