View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.security.MessageDigest;
27  
28  /**
29   * Encode an MD5 digest into a String. <p>
30   *
31   * The 128 bit MD5 hash is converted into a 32 character long String. Each
32   * character of the String is the hexadecimal representation of 4 bits of the
33   * digest.
34   *
35   * XXX The API here is a mess.  It is combining a static utility class with a
36   *     message digest API. Some methods which should be static are not, presumably
37   *     so Jelly can easily access them.
38   *
39   * @author Remy Maucherat
40   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
41   * @version $Revision: 517014 $ $Date: 2007-03-11 22:15:50 +0100 (Sun, 11 Mar 2007) $
42   */
43  public class MD5Sum
44  {
45      /** File that will be checksummed. */
46      private File file;
47  
48      /** Checksum */
49      private String checksum;
50  
51      /** Hex digits. */
52      private static final char[] hexadecimal = {
53          '0',
54          '1',
55          '2',
56          '3',
57          '4',
58          '5',
59          '6',
60          '7',
61          '8',
62          '9',
63          'a',
64          'b',
65          'c',
66          'd',
67          'e',
68          'f' };
69  
70      /** Set the file to perform the checksum on.
71       *
72       *  @param file The file.
73       */
74      public void setFile( File file )
75      {
76          this.file = file;
77      }
78  
79      /** Get the file that the checksum will be perform on.
80       *
81       *  @return the file.
82       */
83      public File getFile()
84      {
85          return file;
86      }
87  
88      /** Set the checksum
89       *
90       *  @param checksum The checksum.
91       */
92      public void setChecksum( String checksum )
93      {
94          this.checksum = checksum;
95      }
96  
97      /** Get the checksum
98       *
99       *  @return The calculated checksum.
100      */
101     public String getChecksum()
102     {
103         return checksum;
104     }
105 
106     /**
107      * Encodes the 128 bit (16 bytes) MD5 into a 32 character String.
108      *
109      * @param binaryData Array containing the digest
110      *
111      * @return Encoded MD5, or null if encoding failed
112      */
113     public String encode( byte[] binaryData )
114     {
115 
116         if ( binaryData.length != 16 )
117         {
118             return null;
119         }
120 
121         char[] buffer = new char[32];
122 
123         for ( int i = 0; i < 16; i++ )
124         {
125             int low = ( binaryData[i] & 0x0f );
126             int high = ( ( binaryData[i] & 0xf0 ) >> 4 );
127             buffer[i * 2] = hexadecimal[high];
128             buffer[i * 2 + 1] = hexadecimal[low];
129         }
130 
131         return new String( buffer );
132     }
133 
134     /** Pull in static content and store it
135      *
136      *  @param file The file to read.
137      *
138      *  @return The bytes of the file.
139      *
140      *  @throws Exception If an error occurs reading in the file's bytes.
141      */
142     public byte[] getBytes( File file )
143         throws Exception
144     {
145         ByteArrayOutputStream baos = new ByteArrayOutputStream();
146         InputStream stream = new FileInputStream( file );
147 
148         byte buf[] = new byte[1024];
149         int len = 0;
150 
151         while ( ( len = stream.read( buf, 0, 1024 ) ) != -1 )
152         {
153             baos.write( buf, 0, len );
154         }
155 
156         try
157         {
158             stream.close();
159         }
160         catch ( IOException e )
161         {
162             // Nothing to do
163         }
164 
165         return baos.toByteArray();
166     }
167 
168     /** Perform the MD5-Sum work.
169      *
170      *  @throws Exception If an error occurs while calculating the sum.
171      */
172     public void execute()
173         throws Exception
174     {
175         if ( !file.exists() )
176         {
177             System.err.println( "Specified file " + file + " doesn't exist." );
178         }
179 
180         MessageDigest md5Helper = MessageDigest.getInstance( "MD5" );
181         setChecksum( encode( md5Helper.digest( getBytes( file ) ) ) );
182     }
183 }