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

com.hfg.util.io.MD5Checksum Maven / Gradle / Ivy

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

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


//------------------------------------------------------------------------------
/**
 Calculates/validates MD5 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 MD5Checksum
{

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

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

   //--------------------------------------------------------------------------
   public static byte[] calculate(InputStream inStream)
   {
      MessageDigest md5 = null;

      try
      {
         md5 = MessageDigest.getInstance("MD5");

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

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

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

      return md5.digest();
   }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy