package com.renrenliao.im.utils;

import android.text.TextUtils;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

/**
 * *@ClassName DigestUtils
 * *@Description TODO
 * *@Author ganzhe
 * *@Date 2022/5/21 16:45
 **/
public class DigestUtils {
    /**
     * 使用 md5 加密
     *
     * @param string
     * @return
     */
    public static String md5(String string) {
        if(StringUtils.isEmpty(string)){
            return "";
        }
        byte[] hash = null;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (Exception e) {
//            throw new RuntimeException("没有md5这个算法！");
        }
        if (hash != null) {
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (int i = 0; i < hash.length; i++) {
                //0xFF 表示 255
                if ((hash[i] & 0xFF) < 0x10){
                    hex.append("0");
                }
                hex.append(Integer.toHexString(hash[i] & 0xFF));
            }
            return hex.toString();
        } else {
            return "";
        }

    }

    /**
     * 另一种md5的转化法则
     * @param plainText
     * @return
     */
    public static String md5_method1(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
        // 如果生成数字未满32位，需要前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }

    /**
     * base64加密
     * @param str
     * @return
     */
    public static String getBase64(String str) {
        if(TextUtils.isEmpty(str)){
            return str;
        }
        byte[] b = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //    String ss = Base64.encodeToString(b, Base64.DEFAULT);
        String ss = Base64.encodeToString(b, Base64.NO_WRAP);
        return ss;
    }

    /**
     * 取SHA1
     * @param data 数据
     * @return 对应的hash值
     */
    public static byte[] getHashByString(String data)
    {
        try{
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.reset();
            messageDigest.update(data.getBytes("UTF-8"));
            return messageDigest.digest();
        } catch (Exception e){
            return "".getBytes();
        }
    }

    /**
     * 转16进制字符串
     * @param data 数据
     * @return 16进制字符串
     */
    public static String bytesToHexStr(byte[] data){
        StringBuilder sb = new StringBuilder();
        String stmp;
        for (int n = 0; n < data.length; n++){
            stmp = (Integer.toHexString(data[n] & 0xFF));
            if (stmp.length() == 1) {
                sb.append("0");
            }
            sb.append(stmp);
        }
        return sb.toString().toUpperCase(Locale.CHINA);
    }

}
