1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.crypto; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.io.ByteArrayOutputStream; |
24 | import java.io.IOException; |
25 | import java.io.InputStream; |
26 | import java.io.UnsupportedEncodingException; |
27 | import java.security.MessageDigest; |
28 | import java.security.NoSuchAlgorithmException; |
29 | import java.util.Arrays; |
30 | |
31 | import javax.crypto.Mac; |
32 | |
33 | import org.jclouds.encryption.internal.Base64; |
34 | import org.jclouds.io.InputSuppliers; |
35 | |
36 | import com.google.common.annotations.Beta; |
37 | import com.google.common.base.Charsets; |
38 | import com.google.common.base.Throwables; |
39 | import com.google.common.io.ByteProcessor; |
40 | import com.google.common.io.ByteStreams; |
41 | import com.google.common.io.InputSupplier; |
42 | |
43 | /** |
44 | * functions related to but not in {@link com.google.common.io.ByteStreams} |
45 | * |
46 | * @author Adrian Cole |
47 | */ |
48 | @Beta |
49 | public class CryptoStreams { |
50 | |
51 | public static String hex(byte[] in) { |
52 | byte[] hex = new byte[2 * in.length]; |
53 | int index = 0; |
54 | |
55 | for (byte b : in) { |
56 | int v = b & 0xFF; |
57 | hex[index++] = HEX_CHAR_TABLE[v >>> 4]; |
58 | hex[index++] = HEX_CHAR_TABLE[v & 0xF]; |
59 | } |
60 | try { |
61 | return new String(hex, "ASCII"); |
62 | } catch (UnsupportedEncodingException e) { |
63 | throw new RuntimeException(e); |
64 | } |
65 | } |
66 | |
67 | public static byte[] hex(String s) { |
68 | int len = s.length(); |
69 | byte[] data = new byte[len / 2]; |
70 | for (int i = 0; i < len; i += 2) { |
71 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); |
72 | } |
73 | return data; |
74 | } |
75 | |
76 | public static String base64(byte[] in) { |
77 | return Base64.encodeBytes(in, Base64.DONT_BREAK_LINES); |
78 | } |
79 | |
80 | public static byte[] base64(String in) { |
81 | return Base64.decode(in); |
82 | } |
83 | |
84 | /** |
85 | * @see #md5 |
86 | * @see #hex |
87 | */ |
88 | public static String md5Hex(InputSupplier<? extends InputStream> supplier) throws IOException { |
89 | return hex(md5(supplier)); |
90 | } |
91 | |
92 | /** |
93 | * @see #md5 |
94 | * @see #base64 |
95 | */ |
96 | public static String md5Base64(InputSupplier<? extends InputStream> supplier) throws IOException { |
97 | return base64(md5(supplier)); |
98 | } |
99 | |
100 | /** |
101 | * Computes and returns the MAC value for a supplied input stream. The mac object is reset when |
102 | * this method returns successfully. |
103 | * |
104 | * @param supplier |
105 | * the input stream factory |
106 | * @param mac |
107 | * the mac object |
108 | * @return the result of {@link Mac#doFinal()} after updating the mac object with all of the |
109 | * bytes in the stream and encoding in Base64 |
110 | * @throws IOException |
111 | * if an I/O error occurs |
112 | */ |
113 | public static String macBase64(InputSupplier<? extends InputStream> supplier, final Mac mac) throws IOException { |
114 | return base64(mac(supplier, mac)); |
115 | } |
116 | |
117 | /** |
118 | * Computes and returns the Digest value for a supplied input stream. The digest object is reset |
119 | * when this method returns successfully. |
120 | * |
121 | * @param supplier |
122 | * the input stream factory |
123 | * @param md |
124 | * the digest object |
125 | * @return the result of {@link MessageDigest#digest()} after updating the digest object with all |
126 | * of the bytes in the stream |
127 | * @throws IOException |
128 | * if an I/O error occurs |
129 | */ |
130 | public static byte[] digest(InputSupplier<? extends InputStream> supplier, final MessageDigest md) |
131 | throws IOException { |
132 | return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<byte[]>() { |
133 | public boolean processBytes(byte[] buf, int off, int len) { |
134 | md.update(buf, off, len); |
135 | return true; |
136 | } |
137 | |
138 | public byte[] getResult() { |
139 | return md.digest(); |
140 | } |
141 | }); |
142 | } |
143 | |
144 | /** |
145 | * Computes and returns the MD5 value for a supplied input stream. A digest object is created and |
146 | * disposed of at runtime, consider using {@link #digest} to be more efficient. |
147 | * |
148 | * @param supplier |
149 | * the input stream factory |
150 | * |
151 | * @return the result of {@link MessageDigest#digest()} after updating the md5 object with all of |
152 | * the bytes in the stream |
153 | * @throws IOException |
154 | * if an I/O error occurs |
155 | */ |
156 | public static byte[] md5(InputSupplier<? extends InputStream> supplier) throws IOException { |
157 | try { |
158 | return digest(supplier, MessageDigest.getInstance("MD5")); |
159 | } catch (NoSuchAlgorithmException e) { |
160 | Throwables.propagate(e); |
161 | return null; |
162 | } |
163 | } |
164 | |
165 | public static byte[] md5(byte[] in) throws IOException { |
166 | return md5(ByteStreams.newInputStreamSupplier(in)); |
167 | } |
168 | |
169 | /** |
170 | * Computes and returns the MAC value for a supplied input stream. The mac object is reset when |
171 | * this method returns successfully. |
172 | * |
173 | * @param supplier |
174 | * the input stream factory |
175 | * @param mac |
176 | * the mac object |
177 | * @return the result of {@link Mac#doFinal()} after updating the mac object with all of the |
178 | * bytes in the stream |
179 | * @throws IOException |
180 | * if an I/O error occurs |
181 | */ |
182 | public static byte[] mac(InputSupplier<? extends InputStream> supplier, final Mac mac) throws IOException { |
183 | return com.google.common.io.ByteStreams.readBytes(checkNotNull(supplier, "supplier"), |
184 | new ByteProcessor<byte[]>() { |
185 | public boolean processBytes(byte[] buf, int off, int len) { |
186 | mac.update(buf, off, len); |
187 | return true; |
188 | } |
189 | |
190 | public byte[] getResult() { |
191 | return mac.doFinal(); |
192 | } |
193 | }); |
194 | } |
195 | |
196 | /** |
197 | * Computes and returns the base64 value for a supplied input stream. |
198 | * |
199 | * @param supplier |
200 | * the input stream factory |
201 | * |
202 | * @return the result of base 64 encoding all of the bytes in the stream |
203 | * @throws IOException |
204 | * if an I/O error occurs |
205 | */ |
206 | public static String base64Encode(InputSupplier<? extends InputStream> supplier) throws IOException { |
207 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
208 | return com.google.common.io.ByteStreams.readBytes(InputSuppliers.base64Encoder(supplier), |
209 | new ByteProcessor<String>() { |
210 | public boolean processBytes(byte[] buf, int off, int len) { |
211 | out.write(buf, off, len); |
212 | return true; |
213 | } |
214 | |
215 | public String getResult() { |
216 | return new String(out.toByteArray(), Charsets.UTF_8); |
217 | } |
218 | }); |
219 | } |
220 | |
221 | /** |
222 | * Computes and returns the unencoded value for an input stream which is encoded in Base64. |
223 | * |
224 | * @param supplier |
225 | * the input stream factory |
226 | * |
227 | * @return the result of base 64 decoding all of the bytes in the stream |
228 | * @throws IOException |
229 | * if an I/O error occurs |
230 | */ |
231 | public static byte[] base64Decode(InputSupplier<? extends InputStream> supplier) throws IOException { |
232 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
233 | return com.google.common.io.ByteStreams.readBytes(InputSuppliers.base64Decoder(supplier), |
234 | new ByteProcessor<byte[]>() { |
235 | public boolean processBytes(byte[] buf, int off, int len) { |
236 | out.write(buf, off, len); |
237 | return true; |
238 | } |
239 | |
240 | public byte[] getResult() { |
241 | return out.toByteArray(); |
242 | } |
243 | }); |
244 | } |
245 | |
246 | final static byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', |
247 | (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', |
248 | (byte) 'f' }; |
249 | |
250 | /** |
251 | * Computes and returns the hex value for a supplied input stream. |
252 | * |
253 | * @param supplier |
254 | * the input stream factory |
255 | * |
256 | * @return the result of hex encoding all of the bytes in the stream |
257 | * @throws IOException |
258 | * if an I/O error occurs |
259 | */ |
260 | public static String hexEncode(InputSupplier<? extends InputStream> supplier) throws IOException { |
261 | final StringBuilder out = new StringBuilder(); |
262 | return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<String>() { |
263 | public boolean processBytes(byte[] buf, int off, int len) { |
264 | char[] hex = new char[2 * len]; |
265 | int index = 0; |
266 | |
267 | for (int i = off; i < off + len; i++) { |
268 | byte b = buf[i]; |
269 | int v = b & 0xFF; |
270 | hex[index++] = (char) HEX_CHAR_TABLE[v >>> 4]; |
271 | hex[index++] = (char) HEX_CHAR_TABLE[v & 0xF]; |
272 | } |
273 | out.append(hex); |
274 | return true; |
275 | } |
276 | |
277 | public String getResult() { |
278 | return out.toString(); |
279 | } |
280 | }); |
281 | } |
282 | |
283 | /** |
284 | * Computes and returns the unencoded value for an input stream which is encoded in hex. |
285 | * |
286 | * @param supplier |
287 | * the input stream factory |
288 | * |
289 | * @return the result of hex decoding all of the bytes in the stream |
290 | * @throws IOException |
291 | * if an I/O error occurs |
292 | */ |
293 | public static byte[] hexDecode(InputSupplier<? extends InputStream> supplier) throws IOException { |
294 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
295 | return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<byte[]>() { |
296 | int currentPos = 0; |
297 | |
298 | public boolean processBytes(byte[] buf, int off, int len) { |
299 | try { |
300 | if (currentPos == 0 && new String(Arrays.copyOfRange(buf, off, 2), "ASCII").equals("0x")) { |
301 | off += 2; |
302 | } |
303 | byte[] decoded = hex(new String(Arrays.copyOfRange(buf, off, len), "ASCII")); |
304 | out.write(decoded, 0, decoded.length); |
305 | currentPos += len; |
306 | } catch (UnsupportedEncodingException e) { |
307 | throw new IllegalStateException("ASCII must be supported"); |
308 | } |
309 | return true; |
310 | } |
311 | |
312 | public byte[] getResult() { |
313 | return out.toByteArray(); |
314 | } |
315 | }); |
316 | } |
317 | |
318 | } |