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