MD5Utils.java 源代码


package utils;

import android.text.TextUtils;
import com.baidu.mobads.sdk.internal.bv;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import kotlin.UByte;

public class MD5Utils {
    public static String md5(String str) {
        return toMd5(toMd5(str));
    }

    private static String toMd5(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            byte[] digest = MessageDigest.getInstance(bv.a).digest(str.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & UByte.MAX_VALUE);
                if (hexString.length() == 1) {
                    hexString = PushConstants.PUSH_TYPE_NOTIFY + hexString;
                }
                sb.append(hexString);
            }
            return sb.toString().toUpperCase().substring(6, 19);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String toMd5For4G(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            byte[] digest = MessageDigest.getInstance(bv.a).digest(str.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & UByte.MAX_VALUE);
                if (hexString.length() == 1) {
                    hexString = PushConstants.PUSH_TYPE_NOTIFY + hexString;
                }
                sb.append(hexString);
            }
            return sb.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}