KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > modifiedselector > DigestAlgorithm


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors.modifiedselector;
20
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.security.DigestInputStream JavaDoc;
25 import java.security.MessageDigest JavaDoc;
26 import java.security.NoSuchAlgorithmException JavaDoc;
27 import java.security.NoSuchProviderException JavaDoc;
28 import org.apache.tools.ant.BuildException;
29
30
31 /**
32  * Computes a 'hashvalue' for the content of file using
33  * java.security.MessageDigest.
34  * Use of this algorithm doesn't require any additional nested <param>s.
35  * Supported <param>s are:
36  * <table>
37  * <tr>
38  * <th>name</th><th>values</th><th>description</th><th>required</th>
39  * </tr>
40  * <tr>
41  * <td> algorithm.algorithm </td>
42  * <td> MD5 | SHA (default provider) </td>
43  * <td> name of the algorithm the provider should use </td>
44  * <td> no, defaults to MD5 </td>
45  * </tr>
46  * <tr>
47  * <td> algorithm.provider </td>
48  * <td> </td>
49  * <td> name of the provider to use </td>
50  * <td> no, defaults to <i>null</i> </td>
51  * </tr>
52  * </table>
53  *
54  * @version 2004-07-08
55  * @since Ant 1.6
56  */

57 public class DigestAlgorithm implements Algorithm {
58
59
60     // ----- member variables -----
61

62
63     /**
64      * MessageDigest algorithm to be used.
65      */

66     private String JavaDoc algorithm = "MD5";
67
68     /**
69      * MessageDigest Algorithm provider
70      */

71     private String JavaDoc provider = null;
72
73     /**
74      * Message Digest instance
75      */

76     private MessageDigest JavaDoc messageDigest = null;
77
78     /**
79      * Size of the read buffer to use.
80      */

81     private int readBufferSize = 8 * 1024;
82
83
84     // ----- Algorithm-Configuration -----
85

86
87     /**
88      * Specifies the algorithm to be used to compute the checksum.
89      * Defaults to "MD5". Other popular algorithms like "SHA" may be used as well.
90      * @param algorithm the digest algorithm to use
91      */

92     public void setAlgorithm(String JavaDoc algorithm) {
93         this.algorithm = algorithm;
94     }
95
96
97     /**
98      * Sets the MessageDigest algorithm provider to be used
99      * to calculate the checksum.
100      * @param provider provider to use
101      */

102     public void setProvider(String JavaDoc provider) {
103         this.provider = provider;
104     }
105
106
107     /** Initialize the security message digest. */
108     public void initMessageDigest() {
109         if (messageDigest != null) {
110             return;
111         }
112
113         if ((provider != null) && !"".equals(provider) && !"null".equals(provider)) {
114             try {
115                 messageDigest = MessageDigest.getInstance(algorithm, provider);
116             } catch (NoSuchAlgorithmException JavaDoc noalgo) {
117                 throw new BuildException(noalgo);
118             } catch (NoSuchProviderException JavaDoc noprovider) {
119                 throw new BuildException(noprovider);
120             }
121         } else {
122             try {
123                 messageDigest = MessageDigest.getInstance(algorithm);
124             } catch (NoSuchAlgorithmException JavaDoc noalgo) {
125                 throw new BuildException(noalgo);
126             }
127         }
128     }
129
130
131     // ----- Logic -----
132

133
134     /**
135      * This algorithm supports only MD5 and SHA.
136      * @return <i>true</i> if all is ok, otherwise <i>false</i>.
137      */

138     public boolean isValid() {
139         return "SHA".equalsIgnoreCase(algorithm) || "MD5".equalsIgnoreCase(algorithm);
140     }
141
142
143     /**
144      * Computes a value for a file content with the specified digest algorithm.
145      * @param file File object for which the value should be evaluated.
146      * @return The value for that file
147      */

148     // implementation adapted from ...taskdefs.Checksum, thanks to Magesh for hint
149
public String JavaDoc getValue(File JavaDoc file) {
150         initMessageDigest();
151         String JavaDoc checksum = null;
152         try {
153             if (!file.canRead()) {
154                 return null;
155             }
156             FileInputStream JavaDoc fis = null;
157
158             byte[] buf = new byte[readBufferSize];
159             try {
160                 messageDigest.reset();
161                 fis = new FileInputStream JavaDoc(file);
162                 DigestInputStream JavaDoc dis = new DigestInputStream JavaDoc(fis,
163                                                               messageDigest);
164                 while (dis.read(buf, 0, readBufferSize) != -1) {
165                     // do nothing
166
}
167                 dis.close();
168                 fis.close();
169                 fis = null;
170                 byte[] fileDigest = messageDigest.digest();
171                 StringBuffer JavaDoc checksumSb = new StringBuffer JavaDoc();
172                 for (int i = 0; i < fileDigest.length; i++) {
173                     String JavaDoc hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
174                     if (hexStr.length() < 2) {
175                         checksumSb.append("0");
176                     }
177                     checksumSb.append(hexStr);
178                 }
179                 checksum = checksumSb.toString();
180             } catch (Exception JavaDoc e) {
181                 return null;
182             }
183         } catch (Exception JavaDoc e) {
184             return null;
185         }
186         return checksum;
187     }
188
189
190     /**
191      * Override Object.toString().
192      * @return some information about this algorithm.
193      */

194     public String JavaDoc toString() {
195         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
196         buf.append("<DigestAlgorithm:");
197         buf.append("algorithm=").append(algorithm);
198         buf.append(";provider=").append(provider);
199         buf.append(">");
200         return buf.toString();
201     }
202 }
203
Popular Tags