開發必備:MD5 加密工具、非空判斷工具

MD5 加密工具

public class MD5Util {

    public static String encrypt(String source) {
        return encodeMd5(source.getBytes());
    }

    private static String encodeMd5(byte[] source) {
        try {
            return encodeHex(MessageDigest.getInstance("MD5").digest(source));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    private static String encodeHex(byte[] bytes) {
        StringBuffer buffer = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10)
                buffer.append("0");
            buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buffer.toString();
    }

}

非空判斷工具

public final class ToolUtils {

    private ToolUtils(){};

    /*
        判斷字符串是否爲空
     */
    public static boolean isEmpty(String src){
        if(src != null && src.trim().length() > 0){
            return false;
        }
        return true;
    }

    /*
        判斷字符串是否不爲空
     */
    public static boolean isNotEmpty(String src){
        if(src == null || src.trim().length() == 0){
            return false;
        }
        return true;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章