package org.dromara.payment.util;

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

public class UniqueIdGenerator {

    public static String generateUniqueId(long input) {
        try {
            // 将long转换为byte数组
            byte[] inputBytes = java.nio.ByteBuffer.allocate(8).putLong(input).array();

            // 使用MD5哈希函数
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(inputBytes);

            // 将哈希值的前5个字节转换为十六进制字符串，并截取前5位
            StringBuilder hexString = new StringBuilder();
            for (byte b : digest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.substring(0, 5); // 取前5位
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        long input = 1811245410163494912L;
        String uniqueId = generateUniqueId(input);
        System.out.println(uniqueId);
    }
}
