001package com.hfg.util;
002
003
004import com.hfg.exception.ProgrammingException;
005
006import java.io.ByteArrayInputStream;
007import java.io.InputStream;
008import java.io.UnsupportedEncodingException;
009import java.security.MessageDigest;
010
011//------------------------------------------------------------------------------
012/**
013 Functions for calculating and validating common checksums.
014 @author J. Alex Taylor, hairyfatguy.com
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// [email protected]
035//------------------------------------------------------------------------------
036
037public class ChecksumUtil
038{
039   //##########################################################################
040   // PUBLIC FUNCTIONS
041   //##########################################################################
042
043   //--------------------------------------------------------------------------
044   public static byte[] calculateSHA1(String inString)
045   {
046      return calculateSHA1(new ByteArrayInputStream(getBytes(inString)));
047   }
048
049   //--------------------------------------------------------------------------
050   public static byte[] calculateSHA1(InputStream inStream)
051   {
052      return calculate(inStream, "SHA-1");
053   }
054
055   //--------------------------------------------------------------------------
056   public static boolean validateSHA1(String inString, byte[] inChecksum)
057   {
058      return validateSHA1(new ByteArrayInputStream(getBytes(inString)), inChecksum);
059   }
060
061   //--------------------------------------------------------------------------
062   public static boolean validateSHA1(InputStream inStream, byte[] inChecksum)
063   {
064      String checksum1 = new String(calculateSHA1(inStream));
065
066      return checksum1.equals(new String(inChecksum));
067   }
068
069   //--------------------------------------------------------------------------
070   public static byte[] calculateMD5(String inString)
071   {
072      return calculateMD5(new ByteArrayInputStream(getBytes(inString)));
073   }
074
075   //--------------------------------------------------------------------------
076   public static byte[] calculateMD5(InputStream inStream)
077   {
078      return calculate(inStream, "MD5");
079   }
080
081   //--------------------------------------------------------------------------
082   public static boolean validateMD5(String inString, byte[] inChecksum)
083   {
084      return validateMD5(new ByteArrayInputStream(getBytes(inString)), inChecksum);
085   }
086
087   //--------------------------------------------------------------------------
088   public static boolean validateMD5(InputStream inStream, byte[] inChecksum)
089   {
090      String checksum1 = new String(calculateMD5(inStream));
091
092      return checksum1.equals(new String(inChecksum));
093   }
094
095   //##########################################################################
096   // PRIVATE FUNCTIONS
097   //##########################################################################
098
099   //--------------------------------------------------------------------------
100   private static byte[] calculate(InputStream inStream, String inDigestMethod)
101   {
102      MessageDigest digest;
103
104      try
105      {
106         digest = MessageDigest.getInstance(inDigestMethod);
107
108         byte[] buffer = new byte[1024];
109         int readSize;
110
111         while ((readSize = inStream.read(buffer)) > 0)
112         {
113            digest.update(buffer, 0, readSize);
114         }
115
116         inStream.close();
117      }
118      catch (Exception e)
119      {
120         throw new RuntimeException("Problem calculating " + inDigestMethod + " checksum!", e);
121      }
122
123      return digest.digest();
124   }
125
126   //--------------------------------------------------------------------------
127   private static byte[] getBytes(String inString)
128   {
129      byte[] bytes = null;
130      try
131      {
132         bytes = inString.getBytes("UTF-8");
133      }
134      catch (UnsupportedEncodingException e)
135      {
136         throw new ProgrammingException(e);
137      }
138
139      return bytes;
140   }
141
142}