package com.zanxiang.game.module.manage.utils;

import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @author : lingfeng
 * @time : 2022-09-23
 * @description : 签名算法工具类
 */
@Slf4j
public class SignUtil {

    /**
     * MD5加密
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String md5(String data) {
        try {
            java.security.MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
            }
            return sb.toString();
        } catch (Exception e) {
            log.error("MD5加密异常, data : {}", data);
            throw new BaseException("MD5加密异常");
        }
    }
}
