MD5.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * @(#)MD5.java 0.3-3 06/05/2001
  3.  *
  4.  *  This file is part of the HTTPClient package
  5.  *  Copyright (C) 1996-2001 Ronald Tschal鋜
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Lesser General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Lesser General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Lesser General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20.  *  MA 02111-1307, USA
  21.  *
  22.  *  For questions, suggestions, bug-reports, enhancement-requests etc.
  23.  *  I may be contacted at:
  24.  *
  25.  *  ronald@innovation.ch
  26.  *
  27.  *  The HTTPClient's home page is located at:
  28.  *
  29.  *  http://www.innovation.ch/java/HTTPClient/ 
  30.  *
  31.  */
  32. package HTTPClient;
  33. import java.io.UnsupportedEncodingException;
  34. import java.security.MessageDigest;
  35. import java.security.NoSuchAlgorithmException;
  36. /**
  37.  * Some utility methods for digesting info using MD5.
  38.  *
  39.  * @version 0.3-3  06/05/2001
  40.  * @author Ronald Tschal鋜
  41.  * @since V0.3-3
  42.  */
  43. class MD5
  44. {
  45.     private static final char[] hex = {
  46. '0', '1', '2', '3', '4', '5', '6', '7',
  47. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  48.     };
  49.     /**
  50.      * Turns array of bytes into string representing each byte as
  51.      * unsigned hex number.
  52.      *
  53.      * @param hash array of bytes to convert to hex-string
  54.      * @return generated hex string
  55.      */
  56.     public static final String toHex(byte hash[])
  57.     {
  58. StringBuffer buf = new StringBuffer(hash.length * 2);
  59. for (int idx=0; idx<hash.length; idx++)
  60.     buf.append(hex[(hash[idx] >> 4) & 0x0f]).append(hex[hash[idx] & 0x0f]);
  61. return buf.toString();
  62.     }
  63.     /**
  64.      * Digest the input.
  65.      *
  66.      * @param input the data to be digested.
  67.      * @return the md5-digested input
  68.      */
  69.     public static final byte[] digest(byte[] input)
  70.     {
  71. try 
  72. {
  73.     MessageDigest md5 = MessageDigest.getInstance("MD5");
  74.     return md5.digest(input);
  75. }
  76. catch (NoSuchAlgorithmException nsae)
  77. {
  78.     throw new Error(nsae.toString());
  79. }
  80.     }
  81.     /**
  82.      * Digest the input.
  83.      *
  84.      * @param input1 the first part of the data to be digested.
  85.      * @param input2 the second part of the data to be digested.
  86.      * @return the md5-digested input
  87.      */
  88.     public static final byte[] digest(byte[] input1, byte[] input2)
  89.     {
  90. try 
  91. {
  92.     MessageDigest md5 = MessageDigest.getInstance("MD5");
  93.     md5.update(input1);
  94.     return md5.digest(input2);
  95. }
  96. catch (NoSuchAlgorithmException nsae)
  97. {
  98.     throw new Error(nsae.toString());
  99. }
  100.     }
  101.     /**
  102.      * Digest the input.
  103.      *
  104.      * @param input the data to be digested.
  105.      * @return the md5-digested input as a hex string
  106.      */
  107.     public static final String hexDigest(byte[] input)
  108.     {
  109. return toHex(digest(input));
  110.     }
  111.     /**
  112.      * Digest the input.
  113.      *
  114.      * @param input1 the first part of the data to be digested.
  115.      * @param input2 the second part of the data to be digested.
  116.      * @return the md5-digested input as a hex string
  117.      */
  118.     public static final String hexDigest(byte[] input1, byte[] input2)
  119.     {
  120. return toHex(digest(input1, input2));
  121.     }
  122.     /**
  123.      * Digest the input.
  124.      *
  125.      * @param input the data to be digested.
  126.      * @return the md5-digested input as a hex string
  127.      */
  128.     public static final byte[] digest(String input)
  129.     {
  130. try
  131.     { return digest(input.getBytes("8859_1")); }
  132. catch (UnsupportedEncodingException uee)
  133.     { throw new Error(uee.toString()); }
  134.     }
  135.     /**
  136.      * Digest the input.
  137.      *
  138.      * @param input the data to be digested.
  139.      * @return the md5-digested input as a hex string
  140.      */
  141.     public static final String hexDigest(String input)
  142.     {
  143. try
  144.     { return toHex(digest(input.getBytes("8859_1"))); }
  145. catch (UnsupportedEncodingException uee)
  146.     { throw new Error(uee.toString()); }
  147.     }
  148. }