All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.ChecksumUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;


import com.hfg.exception.ProgrammingException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

//------------------------------------------------------------------------------
/**
 Functions for calculating and validating common checksums.
 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class ChecksumUtil
{
   //##########################################################################
   // PUBLIC FUNCTIONS
   //##########################################################################

   //--------------------------------------------------------------------------
   public static byte[] calculateSHA1(String inString)
   {
      return calculateSHA1(new ByteArrayInputStream(getBytes(inString)));
   }

   //--------------------------------------------------------------------------
   public static byte[] calculateSHA1(InputStream inStream)
   {
      return calculate(inStream, "SHA-1");
   }

   //--------------------------------------------------------------------------
   public static boolean validateSHA1(String inString, byte[] inChecksum)
   {
      return validateSHA1(new ByteArrayInputStream(getBytes(inString)), inChecksum);
   }

   //--------------------------------------------------------------------------
   public static boolean validateSHA1(InputStream inStream, byte[] inChecksum)
   {
      String checksum1 = new String(calculateSHA1(inStream));

      return checksum1.equals(new String(inChecksum));
   }

   //--------------------------------------------------------------------------
   public static byte[] calculateMD5(String inString)
   {
      return calculateMD5(new ByteArrayInputStream(getBytes(inString)));
   }

   //--------------------------------------------------------------------------
   public static byte[] calculateMD5(InputStream inStream)
   {
      return calculate(inStream, "MD5");
   }

   //--------------------------------------------------------------------------
   public static boolean validateMD5(String inString, byte[] inChecksum)
   {
      return validateMD5(new ByteArrayInputStream(getBytes(inString)), inChecksum);
   }

   //--------------------------------------------------------------------------
   public static boolean validateMD5(InputStream inStream, byte[] inChecksum)
   {
      String checksum1 = new String(calculateMD5(inStream));

      return checksum1.equals(new String(inChecksum));
   }

   //##########################################################################
   // PRIVATE FUNCTIONS
   //##########################################################################

   //--------------------------------------------------------------------------
   private static byte[] calculate(InputStream inStream, String inDigestMethod)
   {
      MessageDigest digest;

      try
      {
         digest = MessageDigest.getInstance(inDigestMethod);

         byte[] buffer = new byte[1024];
         int readSize;

         while ((readSize = inStream.read(buffer)) > 0)
         {
            digest.update(buffer, 0, readSize);
         }

         inStream.close();
      }
      catch (Exception e)
      {
         throw new RuntimeException("Problem calculating " + inDigestMethod + " checksum!", e);
      }

      return digest.digest();
   }

   //--------------------------------------------------------------------------
   private static byte[] getBytes(String inString)
   {
      byte[] bytes = null;
      try
      {
         bytes = inString.getBytes("UTF-8");
      }
      catch (UnsupportedEncodingException e)
      {
         throw new ProgrammingException(e);
      }

      return bytes;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy