java笔记_4_MD5加密

String md5Str = StringUtil.md5Encrypt(md5BeforeStr);
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import java.security.MessageDigest;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    private static final Pattern CAMEL_CASE = Pattern.compile("([A-Z][a-z])|([a-z][A-Z])");

    public StringUtil() {
    }

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String md5Encrypt(String str) {
        if (isEmpty(str)) {
            return null;
        } else {
            MessageDigest md5;
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (Exception var8) {
                var8.printStackTrace();
                return "";
            }

            char[] charArray = str.toCharArray();
            byte[] byteArray = new byte[charArray.length];

            for(int i = 0; i < charArray.length; ++i) {
                byteArray[i] = (byte)charArray[i];
            }

            byte[] md5Bytes = md5.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();

            for(int i = 0; i < md5Bytes.length; ++i) {
                int val = md5Bytes[i] & 255;
                if (val < 16) {
                    hexValue.append("0");
                }

                hexValue.append(Integer.toHexString(val));
            }

            return hexValue.toString();
        }
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }

    public static String camelCase2LineSeparation(String camelWord) {
        Matcher matcher = CAMEL_CASE.matcher(camelWord);
        StringBuffer word = new StringBuffer();

        while(matcher.find()) {
            matcher.appendReplacement(word, matcher.group(0).replaceAll("(.$)", "_$1"));
        }

        matcher.appendTail(word);
        return word.toString().toLowerCase();
    }
}

 

posted @ 2022-08-03 15:27  LuLuYaa  阅读(22)  评论(0编辑  收藏  举报