##### Coverage Report - org.owasp.dependencycheck.utils.Checksum

Classes in this File | Line Coverage | Branch Coverage | Complexity
Checksum |
84% | 21/25
90% | 9/10
| 2.75;2.75

1 |  |
     package org.owasp.dependencycheck.utils;
2 |  |
3 |  |
     import java.io.File;
4 |  |
     import java.io.FileInputStream;
5 |  |
     import java.io.IOException;
6 |  |
     import java.io.InputStream;
7 |  |
     import java.security.MessageDigest;
8 |  |
     import java.security.NoSuchAlgorithmException;
9 |  |
     import java.util.logging.Level;
10 |  |
     import java.util.logging.Logger;
11 |  |
12 |  |
     /**
13 |  |
      * Includes methods to generate the MD5 and SHA1 checksum.
14 |  |
      *
15 |  |
      * This code was copied from Real's How To. It has been slightly modified.
16 |  |
      *
17 |  |
      * Written and compiled by Réal Gagnon ©1998-2012
18 |  |
      *
19 |  |
      * @author Real's How To: http://www.rgagnon.com/javadetails/java-0416.html
20 |  |
      *
21 |  |
      */
22 | 0 |
     public class Checksum {
23 |  |
24 |  |
         /**
25 |  |
          * <p>Creates the cryptographic checksum of a given file using the specified
26 |  |
          * algorithm.</p> <p>This algorithm was copied and heavily modified from
27 |  |
          * Real's How To: http://www.rgagnon.com/javadetails/java-0416.html</p>
28 |  |
          *
29 |  |
          * @param algorithm the algorithm to use to calculate the checksum
30 |  |
          * @param file the file to calculate the checksum for
31 |  |
          * @return the checksum
32 |  |
          * @throws IOException when the file does not exist
33 |  |
          * @throws NoSuchAlgorithmException when an algorithm is specified that does
34 |  |
          * not exist
35 |  |
          */
36 |  |
         public static byte[] getChecksum(String algorithm, File file) throws NoSuchAlgorithmException, IOException {
37 | 75 |
             InputStream fis = null;
38 | 75 |
             byte[] buffer = new byte[1024];
39 | 75 |
             MessageDigest complete = MessageDigest.getInstance(algorithm);
40 |  |
             int numRead;
41 |  |
             try {
42 | 74 |
                 fis = new FileInputStream(file);
43 |  |
                 do {
44 | 39350 |
                     numRead = fis.read(buffer);
45 | 39350 |
                     if (numRead > 0) {
46 | 39277 |
                         complete.update(buffer, 0, numRead);
47 |  |
                     }
48 | 39350 |
                 } while (numRead != -1);
49 |  |
             } finally {
50 | 74 |
                 if (fis != null) {
51 |  |
                     try {
52 | 73 |
                         fis.close();
53 | 0 |
                     } catch (IOException ex) {
54 | 0 |
                         Logger.getLogger(Checksum.class.getName()).log(Level.FINEST, "Error closing file '" + file.getName() + "'.", ex);
55 | 74 |
                     }
56 |  |
                 }
57 |  |
             }
58 | 73 |
             return complete.digest();
59 |  |
         }
60 |  |
61 |  |
         /**
62 |  |
          * Calculates the MD5 checksum of a specified file.
63 |  |
          *
64 |  |
          * @param file the file to generate the MD5 checksum
65 |  |
          * @return the hex representation of the MD5 hash
66 |  |
          * @throws IOException when the file passed in does not exist
67 |  |
          * @throws NoSuchAlgorithmException when the MD5 algorithm is not available
68 |  |
          */
69 |  |
         public static String getMD5Checksum(File file) throws IOException, NoSuchAlgorithmException {
70 | 36 |
             byte[] b = getChecksum("MD5", file);
71 | 36 |
             return getHex(b);
72 |  |
         }
73 |  |
74 |  |
         /**
75 |  |
          * Calculates the SHA1 checksum of a specified file.
76 |  |
          *
77 |  |
          * @param file the file to generate the MD5 checksum
78 |  |
          * @return the hex representation of the SHA1 hash
79 |  |
          * @throws IOException when the file passed in does not exist
80 |  |
          * @throws NoSuchAlgorithmException when the SHA1 algorithm is not available
81 |  |
          */
82 |  |
         public static String getSHA1Checksum(File file) throws IOException, NoSuchAlgorithmException {
83 | 36 |
             byte[] b = getChecksum("SHA1", file);
84 | 36 |
             return getHex(b);
85 |  |
         }
86 |  |
         private static final String HEXES = "0123456789ABCDEF";
87 |  |
88 |  |
         /**
89 |  |
          * <p>Converts a byte array into a hex string.</p>
90 |  |
          *
91 |  |
          * <p>This method was copied from <a
92 |  |
          * href="http://www.rgagnon.com/javadetails/java-0596.html">http://www.rgagnon.com/javadetails/java-0596.html</a></p>
93 |  |
          *
94 |  |
          * @param raw a byte array
95 |  |
          * @return the hex representation of the byte array
96 |  |
          */
97 |  |
         public static String getHex(byte[] raw) {
98 | 73 |
             if (raw == null) {
99 | 0 |
                 return null;
100 |  |
             }
101 | 73 |
             final StringBuilder hex = new StringBuilder(2 * raw.length);
102 | 1386 |
             for (final byte b : raw) {
103 | 1313 |
                 hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
104 |  |
             }
105 | 73 |
             return hex.toString();
106 |  |
         }
107 |  |
     }