天行健,君子以自強不息

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            12 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks
          package com.yill;

          import java.security.MessageDigest;
          import java.security.NoSuchAlgorithmException;

          /**
           * MD5 encrypt class, returns a MD5-encrypted hex string.
           * 
           * 
          @author yill
           * 
          @version 2008-2-26
           * 
          @since 1.0
           
          */

          public class YillMD5
          {
              
          /**
               * The hex digits.
               
          */

              
          private static final String[] hexDigits =
              
          {
                      
          "0""1""2""3""4""5""6""7""8""9""a""b""c",
                      
          "d""e""f"
              }
          ;

              
          /**
               * Transform the byte array to hex string.
               * 
               * 
          @param b
               * 
          @return
               
          */

              
          public static String byteArrayToHexString(byte[] b)
              
          {
                  StringBuffer resultSb 
          = new StringBuffer();
                  
          for (int i = 0; i < b.length; i++)
                  
          {
                      resultSb.append(byteToHexString(b[i]));
                  }

                  
          return resultSb.toString();
              }


              
          /**
               * Transform a byte to hex string.
               * 
               * 
          @param b
               * 
          @return
               
          */

              
          private static String byteToHexString(byte b)
              
          {
                  
          int n = b;
                  
          if (n < 0)
                      n 
          = 256 + n;

                  
          // get the first four bit
                  int d1 = n / 16;

                  
          // get the second four bit
                  int d2 = n % 16;

                  
          return hexDigits[d1] + hexDigits[d2];
              }


              
          /**
               * Get the MD5 encrypt hex string of the origin string. <br/>The origin
               * string won't validate here, so who use the API should validate by
               * himself.
               * 
               * 
          @param origin
               * 
          @return
               * 
          @throws NoSuchAlgorithmException
               
          */

              
          public static String MD5Encode(String origin)
                      
          throws NoSuchAlgorithmException
              
          {
                  MessageDigest md 
          = MessageDigest.getInstance("MD5");
                  
          return byteArrayToHexString(md.digest(origin.getBytes()));
              }


              
          public static void main(String[] args)
              
          {
                  
          try
                  
          {
                      System.out
                              .println(
          "The MD5 encrypt code of http://www.aygfsteel.com/yill/ is: "
                                      
          + YillMD5
                                              .MD5Encode(
          "http://www.aygfsteel.com/yill/"));
                  }

                  
          catch (NoSuchAlgorithmException e)
                  
          {
                      e.printStackTrace();
                  }

              }

          }

          posted on 2008-03-18 23:42 yill 閱讀(448) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 崇文区| 全南县| 色达县| 九江县| 泾阳县| 永定县| 额敏县| 宣恩县| 元氏县| 天峨县| 中宁县| 交城县| 丹阳市| 广饶县| 新巴尔虎左旗| 紫云| 双城市| 灵璧县| 慈溪市| 中阳县| 宁夏| 包头市| 云林县| 庆阳市| 东兴市| 万载县| 林口县| 墨脱县| 绵竹市| 拜泉县| 越西县| 繁峙县| 屯昌县| 沈丘县| 岳普湖县| 高密市| 电白县| 历史| 五原县| 常州市| 沛县|