package com.ruoyi.lsfx.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 */
public class MD5Util {

    /**
     * MD5加密
     * @param input 待加密字符串
     * @return MD5加密后的32位小写字符串
     */
    public static String encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5加密失败", e);
        }
    }

    /**
     * 生成安全码
     * @param projectNo 项目编号
     * @param entityName 项目名称
     * @param appSecret 应用密钥
     * @return MD5安全码
     */
    public static String generateSecretCode(String projectNo, String entityName, String appSecret) {
        String raw = projectNo + "_" + entityName + "_" + appSecret;
        return encrypt(raw);
    }
}
