liujie
2026-01-24 13392606c16362d23478e84e0d43e869813d3095
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
package com.estate.expert.admin.utils;
 
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * MD5加密工具类
 */
public class MD5Utils {
 
    /**
     * 对字符串进行MD5加密
     * @param input 输入字符串
     * @return MD5加密后的字符串
     */
    public static String encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte hashByte : hashBytes) {
                String hex = Integer.toHexString(0xff & hashByte);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }
 
    /**
     * 验证密码是否匹配
     * @param inputPassword 输入的密码
     * @param storedHash 存储的MD5哈希值
     * @return 是否匹配
     */
    public static boolean verify(String inputPassword, String storedHash) {
        String inputHash = encrypt(inputPassword);
        return inputHash.equals(storedHash);
    }
}