package com.yc.ship.module.resource.service.resource.util; /**
 * @author ：qsl
 * @description：TODO
 * @date ：2025/8/12 15:15
 **/


import cn.hutool.json.JSONUtil;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public  class EncryptUtils {

    // Base64 ------------------
    public static String base64Enc(String src) {
        if (src == null || src.length() == 0 || src.trim().length() == 0) {
            return "";
        }
        return new String(base64Enc(src.getBytes()));
    }

    public static String base64Dec(String src) {
        if (src == null || src.length() == 0 || src.trim().length() == 0) {
            return "";
        }
        return new String(base64Dec(src.getBytes()));
    }
    public static byte[] base64Enc(byte[] bytes) {
        return Base64.getEncoder().encode(bytes);
    }

    public static byte[] base64Dec(byte[] bytes) {
        return Base64.getDecoder().decode(bytes);
    }

    public static String getMd5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(text.getBytes(StandardCharsets.UTF_8));

        StringBuilder builder = new StringBuilder();

        for (byte aByte : bytes) {
            builder.append(Integer.toHexString((0x000000FF & aByte) | 0xFFFFFF00).substring(6));
        }
        return builder.toString();
    }

    public static Map<String, Object> mapIterators(Map<String, Object> map){
        Map<String, Object> keymap = new TreeMap<String, Object>();

        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            keymap.put(key, map.get(key));
        }
        return keymap;
    }

    public static String mapIterator(Map<String, Object> map){
        Map<String, Object> keymap = new TreeMap<String, Object>();

        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            keymap.put(key, map.get(key));
        }
        return JSONUtil.toJsonStr(keymap);
    }

    /**
     * 获取sign
     * @param params
     * @param secret
     * @return
     */
    public static String getSign(Map<String, String> params, String secret) {
        Map<String, String> sortedParams = new TreeMap(params);
        StringBuilder builder = new StringBuilder();
        builder.append(secret);
        Iterator var4 = sortedParams.entrySet().iterator();

        while(var4.hasNext()) {
            Map.Entry<String, String> param = (Map.Entry)var4.next();
            builder.append((String)param.getKey()).append((String)param.getValue());
        }

        builder.append(secret);
        // md5 加密
        return md5Hex(builder.toString());
    }
    private static String md5Hex(String data) {
        return toHexString(md5(data));
    }

    private static byte[] md5(String data) {
        return getDigest().digest((data.getBytes()));
    }
    private static MessageDigest getDigest() {
        try {
            return MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            throw new RuntimeException(var1);
        }
    }

    private static String toHexString(byte[] b) {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            sb.append("0123456789abcdef".charAt(b[i] >>> 4 & 15));
            sb.append("0123456789abcdef".charAt(b[i] & 15));
        }
        return sb.toString();
    }

}