package com.swago.baseswago.util;

import com.google.android.gms.common.util.Base64Utils;
import com.google.android.gms.common.util.Hex;

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

public class MD5Util {

    /**
     * 获取十六进制字符串形式的MD5摘要
     */
    public static String md5Hex(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bs = md5.digest(src.getBytes());
            return  Hex.bytesToStringLowercase(bs);
        } catch (Exception e) {
            throw new RuntimeException("md5加密失败", e);
        }
    }


    public static String encodeBase64(String str){
        try {
            return Base64Utils.encodeUrlSafe(str.getBytes(StandardCharsets.UTF_8));
        }catch (Exception e){
            throw new RuntimeException("加密失败",e);
        }
    }

    public static String decodeBase64(String str) {
        try {
            return new String(Base64Utils.decodeUrlSafe(str),"UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("解密失败", e);
        }
    }
}
