EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.crypto]

COVERAGE SUMMARY FOR SOURCE FILE [CryptoStreams.java]

nameclass, %method, %block, %line, %
CryptoStreams.java100% (7/7)91%  (32/35)94%  (439/468)85%  (61/72)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CryptoStreams$6100% (1/1)100% (3/3)88%  (53/60)75%  (9/12)
processBytes (byte [], int, int): boolean 100% (1/1)85%  (40/47)67%  (6/9)
CryptoStreams$6 (ByteArrayOutputStream): void 100% (1/1)100% (9/9)100% (2/2)
getResult (): byte [] 100% (1/1)100% (4/4)100% (1/1)
     
class CryptoStreams100% (1/1)82%  (14/17)92%  (249/271)78%  (28/36)
CryptoStreams (): void 0%   (0/1)0%   (0/3)0%   (0/1)
base64 (String): byte [] 0%   (0/1)0%   (0/3)0%   (0/1)
md5 (byte []): byte [] 0%   (0/1)0%   (0/4)0%   (0/1)
md5 (InputSupplier): byte [] 100% (1/1)45%  (5/11)25%  (1/4)
hex (byte []): String 100% (1/1)90%  (52/58)78%  (7/9)
<static initializer> 100% (1/1)100% (68/68)100% (1/1)
base64 (byte []): String 100% (1/1)100% (4/4)100% (1/1)
base64Decode (InputSupplier): byte [] 100% (1/1)100% (13/13)100% (2/2)
base64Encode (InputSupplier): String 100% (1/1)100% (13/13)100% (2/2)
digest (InputSupplier, MessageDigest): byte [] 100% (1/1)100% (8/8)100% (1/1)
hex (String): byte [] 100% (1/1)100% (38/38)100% (5/5)
hexDecode (InputSupplier): byte [] 100% (1/1)100% (12/12)100% (2/2)
hexEncode (InputSupplier): String 100% (1/1)100% (12/12)100% (2/2)
mac (InputSupplier, Mac): byte [] 100% (1/1)100% (11/11)100% (1/1)
macBase64 (InputSupplier, Mac): String 100% (1/1)100% (5/5)100% (1/1)
md5Base64 (InputSupplier): String 100% (1/1)100% (4/4)100% (1/1)
md5Hex (InputSupplier): String 100% (1/1)100% (4/4)100% (1/1)
     
class CryptoStreams$1100% (1/1)100% (3/3)100% (18/18)100% (4/4)
CryptoStreams$1 (MessageDigest): void 100% (1/1)100% (6/6)100% (1/1)
getResult (): byte [] 100% (1/1)100% (4/4)100% (1/1)
processBytes (byte [], int, int): boolean 100% (1/1)100% (8/8)100% (2/2)
     
class CryptoStreams$2100% (1/1)100% (3/3)100% (18/18)100% (4/4)
CryptoStreams$2 (Mac): void 100% (1/1)100% (6/6)100% (1/1)
getResult (): byte [] 100% (1/1)100% (4/4)100% (1/1)
processBytes (byte [], int, int): boolean 100% (1/1)100% (8/8)100% (2/2)
     
class CryptoStreams$3100% (1/1)100% (3/3)100% (22/22)100% (4/4)
CryptoStreams$3 (ByteArrayOutputStream): void 100% (1/1)100% (6/6)100% (1/1)
getResult (): String 100% (1/1)100% (8/8)100% (1/1)
processBytes (byte [], int, int): boolean 100% (1/1)100% (8/8)100% (2/2)
     
class CryptoStreams$4100% (1/1)100% (3/3)100% (18/18)100% (4/4)
CryptoStreams$4 (ByteArrayOutputStream): void 100% (1/1)100% (6/6)100% (1/1)
getResult (): byte [] 100% (1/1)100% (4/4)100% (1/1)
processBytes (byte [], int, int): boolean 100% (1/1)100% (8/8)100% (2/2)
     
class CryptoStreams$5100% (1/1)100% (3/3)100% (61/61)100% (11/11)
CryptoStreams$5 (StringBuilder): void 100% (1/1)100% (6/6)100% (1/1)
getResult (): String 100% (1/1)100% (4/4)100% (1/1)
processBytes (byte [], int, int): boolean 100% (1/1)100% (51/51)100% (9/9)

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 */
19package org.jclouds.crypto;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.io.ByteArrayOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.UnsupportedEncodingException;
27import java.security.MessageDigest;
28import java.security.NoSuchAlgorithmException;
29import java.util.Arrays;
30 
31import javax.crypto.Mac;
32 
33import org.jclouds.encryption.internal.Base64;
34import org.jclouds.io.InputSuppliers;
35 
36import com.google.common.annotations.Beta;
37import com.google.common.base.Charsets;
38import com.google.common.base.Throwables;
39import com.google.common.io.ByteProcessor;
40import com.google.common.io.ByteStreams;
41import 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
49public 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}

[all classes][org.jclouds.crypto]
EMMA 2.0.5312 (C) Vladimir Roubtsov