001         package com.croftsoft.core.security;
002    
003         import java.io.*;
004         import java.security.*;
005    
006         /*********************************************************************
007         * Static method library for creating message digests of byte streams.
008         *
009         * @version
010         *   2003-03-27
011         * @since
012         *   1998-10-04
013         * @author
014         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
015         *********************************************************************/
016    
017         public final class  DigestLib
018         //////////////////////////////////////////////////////////////////////
019         //////////////////////////////////////////////////////////////////////
020         {
021    
022         public static byte [ ]  digest (
023           InputStream  inputStream,
024           String       algorithm )
025           throws IOException, NoSuchAlgorithmException
026         //////////////////////////////////////////////////////////////////////
027         {
028           BufferedInputStream  bufferedInputStream
029             = new BufferedInputStream ( inputStream );
030    
031           MessageDigest  messageDigest
032             = MessageDigest.getInstance ( algorithm );
033    
034           int  i;
035    
036           while ( ( i = bufferedInputStream.read ( ) ) > -1 )
037           {
038             messageDigest.update ( ( byte ) i );
039           }
040    
041           bufferedInputStream.close ( );
042    
043           return messageDigest.digest ( );
044         }
045    
046         public static byte [ ]  digest (
047           File    file,
048           String  algorithm )
049           throws IOException, NoSuchAlgorithmException
050         //////////////////////////////////////////////////////////////////////
051         {
052           return digest ( new FileInputStream ( file ), algorithm );
053         }
054    
055         public static byte [ ]  digest ( File  file )
056           throws IOException, NoSuchAlgorithmException
057         //////////////////////////////////////////////////////////////////////
058         {
059           return digest ( file, "SHA-1" );
060         }
061    
062         //////////////////////////////////////////////////////////////////////
063         //////////////////////////////////////////////////////////////////////
064    
065         private  DigestLib ( ) { }
066    
067         //////////////////////////////////////////////////////////////////////
068         //////////////////////////////////////////////////////////////////////
069         }