package com.qy.agv.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Encryption {

    public static String encrypt(String input) {
        try {
            // 创建 MD5 MessageDigest 实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 计算输入字符串的 MD5 哈希值
            byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // 将字节转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }

            // 确保结果是 32 个字符
            while (sb.length() < 32) {
                sb.append('0');
            }

            // 返回 32 位 MD5 哈希值
            return sb.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 not supported", e);
        }
    }

    public static void main(String[] args) {
        String input = "admin";
        String encrypted = encrypt(input);
        System.out.println("Original: " + input);
        System.out.println("Encrypted (MD5): " + encrypted);
    }
}
