Coverage Report - org.owasp.dependencycheck.utils.Checksum
 
Classes in this File Line Coverage Branch Coverage Complexity
Checksum
88%
22/25
75%
9/12
2.4
 
 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  
 public final class Checksum {
 23  
     
 24  
     /**
 25  
      * The logger.
 26  
      */
 27  1
     private static final Logger LOGGER = Logger.getLogger(Checksum.class.getName());
 28  
     /**
 29  
      * Private constructor for a utility class.
 30  
      */
 31  
     private Checksum() {
 32  
     }
 33  
 
 34  
     /**
 35  
      * <p>Creates the cryptographic checksum of a given file using the specified
 36  
      * algorithm.</p> <p>This algorithm was copied and heavily modified from
 37  
      * Real's How To: http://www.rgagnon.com/javadetails/java-0416.html</p>
 38  
      *
 39  
      * @param algorithm the algorithm to use to calculate the checksum
 40  
      * @param file the file to calculate the checksum for
 41  
      * @return the checksum
 42  
      * @throws IOException when the file does not exist
 43  
      * @throws NoSuchAlgorithmException when an algorithm is specified that does
 44  
      * not exist
 45  
      */
 46  
     public static byte[] getChecksum(String algorithm, File file) throws NoSuchAlgorithmException, IOException {
 47  38
         InputStream fis = null;
 48  38
         byte[] buffer = new byte[1024];
 49  38
         MessageDigest complete = MessageDigest.getInstance(algorithm);
 50  
         int numRead;
 51  
         try {
 52  37
             fis = new FileInputStream(file);
 53  
             do {
 54  13356
                 numRead = fis.read(buffer);
 55  13356
                 if (numRead > 0) {
 56  13323
                     complete.update(buffer, 0, numRead);
 57  
                 }
 58  13356
             } while (numRead != -1);
 59  
         } finally {
 60  37
             if (fis != null) {
 61  
                 try {
 62  33
                     fis.close();
 63  0
                 } catch (IOException ex) {
 64  0
                     LOGGER.log(Level.FINEST, "Error closing file '" + file.getName() + "'.", ex);
 65  37
                 }
 66  
             }
 67  
         }
 68  33
         return complete.digest();
 69  
     }
 70  
 
 71  
     /**
 72  
      * Calculates the MD5 checksum of a specified file.
 73  
      *
 74  
      * @param file the file to generate the MD5 checksum
 75  
      * @return the hex representation of the MD5 hash
 76  
      * @throws IOException when the file passed in does not exist
 77  
      * @throws NoSuchAlgorithmException when the MD5 algorithm is not available
 78  
      */
 79  
     public static String getMD5Checksum(File file) throws IOException, NoSuchAlgorithmException {
 80  19
         byte[] b = getChecksum("MD5", file);
 81  16
         return getHex(b);
 82  
     }
 83  
 
 84  
     /**
 85  
      * Calculates the SHA1 checksum of a specified file.
 86  
      *
 87  
      * @param file the file to generate the MD5 checksum
 88  
      * @return the hex representation of the SHA1 hash
 89  
      * @throws IOException when the file passed in does not exist
 90  
      * @throws NoSuchAlgorithmException when the SHA1 algorithm is not available
 91  
      */
 92  
     public static String getSHA1Checksum(File file) throws IOException, NoSuchAlgorithmException {
 93  16
         byte[] b = getChecksum("SHA1", file);
 94  16
         return getHex(b);
 95  
     }
 96  
     private static final String HEXES = "0123456789ABCDEF";
 97  
 
 98  
     /**
 99  
      * <p>Converts a byte array into a hex string.</p>
 100  
      *
 101  
      * <p>This method was copied from <a
 102  
      * href="http://www.rgagnon.com/javadetails/java-0596.html">http://www.rgagnon.com/javadetails/java-0596.html</a></p>
 103  
      *
 104  
      * @param raw a byte array
 105  
      * @return the hex representation of the byte array
 106  
      */
 107  
     public static String getHex(byte[] raw) {
 108  33
         if (raw == null) {
 109  0
             return null;
 110  
         }
 111  33
         final StringBuilder hex = new StringBuilder(2 * raw.length);
 112  626
         for (final byte b : raw) {
 113  593
             hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
 114  
         }
 115  33
         return hex.toString();
 116  
     }
 117  
 }