java简单实现MD5算法

 

java 代码
  1. package com.ctgusec.bean;   
  2.   
  3. import java.security.MessageDigest;   
  4.   
  5. /** */  
  6. /**  
  7.  * @author zhupan  
  8.  */  
  9. public class MD5 {   
  10.     private String inStr;   
  11.   
  12.     private MessageDigest md5;   
  13.   
  14.     /** */  
  15.     /**  
  16.      * Constructs the MD5 object and sets the string whose MD5 is to be  
  17.      * computed.  
  18.      *   
  19.      * @param inStr  
  20.      *            the <code>String</code> whose MD5 is to be computed  
  21.      */  
  22.     public MD5(String inStr) {   
  23.         this.inStr = inStr;   
  24.         try {   
  25.             this.md5 = MessageDigest.getInstance(" MD5 ");   
  26.         } catch (Exception e) {   
  27.             System.out.println(e.toString());   
  28.             e.printStackTrace();   
  29.         }   
  30.     }   
  31.   
  32.     /** */  
  33.     /**  
  34.      * Computes the MD5 fingerprint of a string.  
  35.      *   
  36.      * @return the MD5 digest of the input <code>String</code>  
  37.      */  
  38.     public String compute() {   
  39.         // convert input String to a char[]   
  40.         // convert that char[] to byte[]   
  41.         // get the md5 digest as byte[]   
  42.         // bit-wise AND that byte[] with 0xff   
  43.         // prepend "0" to the output StringBuffer to make sure that we don't end   
  44.         // up with   
  45.         // something like "e21ff" instead of "e201ff"   
  46.   
  47.         char[] charArray = this.inStr.toCharArray();   
  48.   
  49.         byte[] byteArray = new byte[charArray.length];   
  50.   
  51.         for (int i = 0; i < charArray.length; i++)   
  52.             byteArray[i] = (byte) charArray[i];   
  53.   
  54.         byte[] md5Bytes = this.md5.digest(byteArray);   
  55.   
  56.         StringBuffer hexValue = new StringBuffer();   
  57.   
  58.         for (int i = 0; i < md5Bytes.length; i++) {   
  59.             int val = ((int) md5Bytes[i]) & 0xff;   
  60.             if (val < 16)   
  61.                 hexValue.append(" 0 ");   
  62.             hexValue.append(Integer.toHexString(val));   
  63.         }   
  64.   
  65.         return hexValue.toString();   
  66.     }   
  67.   
  68.     public static void main(String[] args) {   
  69.         MD5 md5 = new MD5(" zhupan ");   
  70.         String postString = md5.compute();   
  71.         System.out.println(postString);   
  72.         if (postString.equals(" 11c454577d6f32b0f3b43fdbf83510a2 ")) {   
  73.             System.out.println(" true ");   
  74.         } else  
  75.             System.out.println(" false ");   
  76.     }   
  77. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值