package com.ho.common.tools.util;

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

public class CheckCodeUtils {

    /**
     * 获取校验码
     * @param str
     * @return
     */
    public static String getCheckCode(String str){
        String checkCode = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            String s = new BigInteger(1, md.digest()).toString(16);
            byte[] hash = md.digest(s.getBytes(StandardCharsets.UTF_8));
            // 使用Base64编码，并在每个部分添加横杠
            StringBuilder sb = new StringBuilder();
            for (byte b : hash) {
                sb.append(String.format("%02x", b));
                if (sb.length() % 5== 0) {
                    sb.append("-");
                }
            }
            checkCode = sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return checkCode;
    }
}
