Java-MD5

作者: 雷哒哒 | 来源:发表于2020-01-16 18:28 被阅读0次

    Java封装转换MD5值程序-MD5Utils

    package cn.lpl666;
    import java.security.MessageDigest;
    public class MD5Utils {
      public static String MD5(String s) {
        try {
          MessageDigest md = MessageDigest.getInstance("MD5");
          byte[] bytes = md.digest(s.getBytes("utf-8"));
          return toHex(bytes);
        }
        catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
      private static String toHex(byte[] bytes) {
    
        final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i=0; i<bytes.length; i++) {
          ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
          ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
      }
    }
    
    

    测试代码

    package cn.lpl666;
    public class Test {
      public static void main(String[] args) {
        String pwd = "123456";
        String md5Value = MD5Utils.MD5(pwd);
        System.out.println(md5Value); // E10ADC3949BA59ABBE56E057F20F883E
      }
    }
    
    

    相关文章

      网友评论

        本文标题:Java-MD5

        本文链接:https://www.haomeiwen.com/subject/tkqqzctx.html