KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > softabar > sha4j > ShaUtil


1 /*
2  * Copyright (C) 2006 Softabar
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, * Inc., * 59 Temple Place, * Suite 330,
17  * Boston, MA 02111-1307 USA
18 */

19 package com.softabar.sha4j;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 /**
28  * Utility methods for Sha4J.<br/>
29  * <pre>
30  * Copyright (C) 2006 Softabar
31  *
32  * This program is free software; you can redistribute it and/or modify it
33  * under the terms of the GNU General Public License as published by the
34  * Free Software Foundation; either version 2 of the License, or
35  * (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful, but
38  * WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program; if not, write to the
44  * Free Software Foundation, * Inc., * 59 Temple Place, * Suite 330,
45  * Boston, MA 02111-1307 USA
46  * </pre>
47  *
48  * @version 1.0
49  */

50 public class ShaUtil
51 {
52   private static Sha4J sha4j=new Sha4J();
53   //SHA-1
54

55   public static byte[] sha1(String JavaDoc input) throws IOException JavaDoc
56   {
57     sha4j.reset();
58     return sha4j.sha1Digest(new ByteArrayInputStream JavaDoc(input.getBytes()));
59     
60   }
61
62   public static byte[] sha1(InputStream JavaDoc input) throws IOException JavaDoc
63   {
64     sha4j.reset();
65     return sha4j.sha1Digest(input);
66   }
67
68   public static byte[] sha1(File JavaDoc input) throws IOException JavaDoc
69   {
70     sha4j.reset();
71     FileInputStream JavaDoc fis=new FileInputStream JavaDoc(input);
72     return sha4j.sha1Digest(fis);
73   }
74
75   public static String JavaDoc toSha1String(String JavaDoc input) throws IOException JavaDoc
76   {
77     return toHexString(sha1(input));
78   }
79
80   public static String JavaDoc toSha1String(InputStream JavaDoc input) throws IOException JavaDoc
81   {
82     return toHexString(sha1(input));
83   }
84
85   public static String JavaDoc toSha1String(File JavaDoc input) throws IOException JavaDoc
86   {
87     return toHexString(sha1(input));
88   }
89
90   //SHA-224
91

92   public static byte[] sha224(String JavaDoc input) throws IOException JavaDoc
93   {
94     sha4j.reset();
95     return sha4j.sha224Digest(new ByteArrayInputStream JavaDoc(input.getBytes()));
96     
97   }
98
99   public static byte[] sha224(InputStream JavaDoc input) throws IOException JavaDoc
100   {
101     sha4j.reset();
102     return sha4j.sha224Digest(input);
103   }
104
105   public static byte[] sha224(File JavaDoc input) throws IOException JavaDoc
106   {
107     sha4j.reset();
108     FileInputStream JavaDoc fis=new FileInputStream JavaDoc(input);
109     return sha4j.sha224Digest(fis);
110   }
111
112   public static String JavaDoc toSha224String(String JavaDoc input) throws IOException JavaDoc
113   {
114     return toHexString(sha224(input));
115   }
116
117   public static String JavaDoc toSha224String(InputStream JavaDoc input) throws IOException JavaDoc
118   {
119     return toHexString(sha224(input));
120   }
121
122   public static String JavaDoc toSha224String(File JavaDoc input) throws IOException JavaDoc
123   {
124     return toHexString(sha224(input));
125   }
126   
127   
128   //SHA-256
129

130   public static byte[] sha256(String JavaDoc input) throws IOException JavaDoc
131   {
132     sha4j.reset();
133     return sha4j.sha256Digest(new ByteArrayInputStream JavaDoc(input.getBytes()));
134     
135   }
136
137   public static byte[] sha256(InputStream JavaDoc input) throws IOException JavaDoc
138   {
139     sha4j.reset();
140     return sha4j.sha256Digest(input);
141   }
142
143   public static byte[] sha256(File JavaDoc input) throws IOException JavaDoc
144   {
145     sha4j.reset();
146     FileInputStream JavaDoc fis=new FileInputStream JavaDoc(input);
147     return sha4j.sha256Digest(fis);
148   }
149
150   public static String JavaDoc toSha256String(String JavaDoc input) throws IOException JavaDoc
151   {
152     return toHexString(sha256(input));
153   }
154
155   public static String JavaDoc toSha256String(InputStream JavaDoc input) throws IOException JavaDoc
156   {
157     return toHexString(sha256(input));
158   }
159
160   public static String JavaDoc toSha256String(File JavaDoc input) throws IOException JavaDoc
161   {
162     return toHexString(sha256(input));
163   }
164
165   //SHA-384
166

167   public static byte[] sha384(String JavaDoc input) throws IOException JavaDoc
168   {
169     sha4j.reset();
170     return sha4j.sha384Digest(new ByteArrayInputStream JavaDoc(input.getBytes()));
171     
172   }
173
174   public static byte[] sha384(InputStream JavaDoc input) throws IOException JavaDoc
175   {
176     sha4j.reset();
177     return sha4j.sha384Digest(input);
178   }
179
180   public static byte[] sha384(File JavaDoc input) throws IOException JavaDoc
181   {
182     sha4j.reset();
183     FileInputStream JavaDoc fis=new FileInputStream JavaDoc(input);
184     return sha4j.sha384Digest(fis);
185   }
186
187   public static String JavaDoc toSha384String(String JavaDoc input) throws IOException JavaDoc
188   {
189     return toHexString(sha384(input));
190   }
191
192   public static String JavaDoc toSha384String(InputStream JavaDoc input) throws IOException JavaDoc
193   {
194     return toHexString(sha384(input));
195   }
196
197   public static String JavaDoc toSha384String(File JavaDoc input) throws IOException JavaDoc
198   {
199     return toHexString(sha384(input));
200   }
201   
202   //SHA-512
203
public static byte[] sha512(String JavaDoc input) throws IOException JavaDoc
204   {
205     sha4j.reset();
206     return sha4j.sha512Digest(new ByteArrayInputStream JavaDoc(input.getBytes()));
207     
208   }
209
210   public static byte[] sha512(InputStream JavaDoc input) throws IOException JavaDoc
211   {
212     sha4j.reset();
213     return sha4j.sha512Digest(input);
214   }
215
216   public static byte[] sha512(File JavaDoc input) throws IOException JavaDoc
217   {
218     sha4j.reset();
219     FileInputStream JavaDoc fis=new FileInputStream JavaDoc(input);
220     return sha4j.sha512Digest(fis);
221   }
222
223   public static String JavaDoc toSha512String(String JavaDoc input) throws IOException JavaDoc
224   {
225     return toHexString(sha512(input));
226   }
227
228   public static String JavaDoc toSha512String(InputStream JavaDoc input) throws IOException JavaDoc
229   {
230     return toHexString(sha512(input));
231   }
232
233   public static String JavaDoc toSha512String(File JavaDoc input) throws IOException JavaDoc
234   {
235     return toHexString(sha512(input));
236   }
237   
238
239   
240   
241   // Display some bytes in HEX.
242

243   private static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
244   
245   /**
246    * Convenience method to print byte array as hexadecimal string.
247    * @param b Byte array to print.
248    * @return Hexadecimal string presentation of byte array.
249    */

250   public static String JavaDoc toHexString(byte[] b)
251   {
252     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
253
254     for (int i = 0; i < b.length; i++)
255     {
256       int c = ((b[i]) >>> 4) & 0xf;
257       sb.append(hex[c]);
258       c = ((int) b[i] & 0xf);
259       sb.append(hex[c]);
260     }
261
262     return sb.toString();
263   }
264 }
265
Popular Tags