KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > examples > Hash


1 /*
2
3 Copyright 2004, Martian Software, Inc.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 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 com.martiansoftware.nailgun.examples;
20
21 import java.security.MessageDigest JavaDoc;
22 import java.security.Provider JavaDoc;
23 import java.security.Security JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import com.martiansoftware.nailgun.NGContext;
28
29 /**
30  * Hashes the client's stdin to the client's stdout in the form of
31  * a hexadecimal string. Command line requires one parameter: either the name
32  * of the algorithm to use (e.g., "MD5"), or "?" to request a list of
33  * available algorithms.
34  *
35  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
36  */

37 public class Hash {
38     
39     // used to turn byte[] to string
40
private static final char[] HEXCHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
41
42     /**
43      * Provides a list of algorithms for the specified service (which, for
44      * our purposes, is "MessageDigest".
45      *
46      * This method was only very slightly adapted (to use a TreeSet) from
47      * the Java Almanac at http://javaalmanac.com/egs/java.security/ListServices.html
48      * @param serviceType The name of the service we're looking for. It's "MessageDigest"
49      */

50     private static Set JavaDoc getCryptoImpls(String JavaDoc serviceType) {
51         Set JavaDoc result = new java.util.TreeSet JavaDoc();
52     
53         // All all providers
54
Provider JavaDoc[] providers = Security.getProviders();
55         for (int i=0; i<providers.length; i++) {
56             // Get services provided by each provider
57
Set JavaDoc keys = providers[i].keySet();
58             for (Iterator JavaDoc it = keys.iterator(); it.hasNext(); ) {
59                 String JavaDoc key = (String JavaDoc)it.next();
60                 key = key.split(" ")[0];
61     
62                 if (key.startsWith(serviceType+".")) {
63                     result.add(key.substring(serviceType.length()+1));
64                 } else if (key.startsWith("Alg.Alias."+serviceType+".")) {
65                     // This is an alias
66
result.add(key.substring(serviceType.length()+11));
67                 }
68             }
69         }
70         return (result);
71     }
72     
73     /**
74      * Hashes client stdin, displays hash result to client stdout.
75      * Requires one command line parameter, either the name of the hash
76      * algorithm to use (e.g., "MD5") or "?" to request a list of
77      * available algorithms. Any exceptions become the problem of the user.
78      */

79     public static void nailMain(NGContext context) throws java.security.NoSuchAlgorithmException JavaDoc, java.io.IOException JavaDoc {
80         String JavaDoc[] args = context.getArgs();
81         
82         if (args.length == 0) {
83             // display available algorithms
84
Set JavaDoc algs = getCryptoImpls("MessageDigest");
85             for (Iterator JavaDoc i = algs.iterator(); i.hasNext();) {
86                 context.out.println(i.next());
87             }
88         } else {
89             // perform the actual hash. throw any exceptions back to the user.
90
MessageDigest JavaDoc md = MessageDigest.getInstance(args[0]);
91             
92             byte[] b = new byte[1024];
93             int bytesRead = context.in.read(b);
94             while (bytesRead != -1) {
95                 md.update(b, 0, bytesRead);
96                 bytesRead = System.in.read(b);
97             }
98             byte[] result = md.digest();
99             
100             // convert hash result to a string of hex characters and print it to the client.
101
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
102             for (int i = 0; i < result.length; ++i) {
103                 buf.append(HEXCHARS[(result[i] >> 4) & 0x0f]);
104                 buf.append(HEXCHARS[result[i] & 0x0f]);
105             }
106             context.out.println(buf);
107         }
108     }
109
110 }
111
Popular Tags