package com.zhongshu.card.server.core.util;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author TRX
 * @date 2024/3/21
 */
public class SecurityUtil {

    public static String getMD5Str(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(s.getBytes("utf-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toHex(byte[] bytes) {
        final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }

    public static String passWord(String password) {
        password = password + "--";
        String encryptedPassword = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(password.getBytes());
            BigInteger no = new BigInteger(1, digest);
            encryptedPassword = no.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedPassword;
    }
}
