package com.ruoyi.database.util;

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

public class MD5Util {

    /**
     * 生成MD5加密字符串（32位小写）
     */
    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes());
            return bytesToHex(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字节数组转十六进制字符串
     */
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

    // 使用示例
    public static void main(String[] args) {
        long l = System.currentTimeMillis() / 1000L;
        System.out.println(l + "");
        String text = "15599026928" + l + "VAR21UEUjhHmeMqiAvA7VYvQLPn5rO2z";
        String md5Hash = md5(text);
        System.out.println("MD5加密结果: " + md5Hash);
        // 输出: b10a8db164e0754105b7a99be72e3fe5
    }
}