Tools - MD5Util (encrypted string)

Four points of writing code:
     1. Identify needs. What to do?
     2. Analysis of ideas. How to do? (1,2,3...)
     3. Determine the steps. Which statements, methods, and objects to use for each idea.
     4. Code implementation. Realize ideas with specific language codes.

Four tips for learning new technologies:
     1. What is the technology?
     2. What are the characteristics of this technology? (use points that need attention)
     3. How is the technology used? (Write Demo)
     4. When will the technology be used? (Usage scenarios in Project)

---------------------- Plan early, prepare early, and finish early. -------------------------

package com.gzqol.hb.mendianguanli.util ;
 import java.io.UnsupportedEncodingException ;
 import java.security.MessageDigest ;
 import java.security.NoSuchAlgorithmException ;
 import java.util.regex.Matcher ;
 import java.util.regex.Pattern ;
 /**
 * 1.MD5 encrypted string (32-bit uppercase)
 * 2.MD5 encrypted string (32-bit lowercase)
 * <p>
 * MD5 online encryption: https://md5jiami.51240.com/
 */
 public class MD5Util {

 


    /**
      * MD5 encrypted string (32-bit uppercase)
      *
      * @param string MD5-encrypted string
      * @return encrypted string (uppercase)
      */
 public static String md5Encrypt32Upper (String string) {
         byte [] hash ;
         try {    
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString().toUpperCase();
    }

    /**
      * MD5 encrypted string (32-bit lowercase)
      *
      * @param string MD5-encrypted string
      * @return encrypted string (lowercase)
      */
 public static String md5Encrypt32Lower (String string) {
         byte [] hash ;
         try {    
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString().toLowerCase();
    }

    /**
      * Unicode Chinese encoding converted to string
      *
      * @param str
 * @return
 */
 public static String unicodeToString (String str) {              
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837021&siteId=291194637