View Javadoc

    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 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          InputStream fis = null;
    38          byte[] buffer = new byte[1024];
    39          MessageDigest complete = MessageDigest.getInstance(algorithm);
    40          int numRead;
    41          try {
    42              fis = new FileInputStream(file);
    43              do {
    44                  numRead = fis.read(buffer);
    45                  if (numRead > 0) {
    46                      complete.update(buffer, 0, numRead);
    47                  }
    48              } while (numRead != -1);
    49          } finally {
    50              if (fis != null) {
    51                  try {
    52                      fis.close();
    53                  } catch (IOException ex) {
    54                      Logger.getLogger(Checksum.class.getName()).log(Level.FINEST, "Error closing file '" + file.getName() + "'.", ex);
    55                  }
    56              }
    57          }
    58          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          byte[] b = getChecksum("MD5", file);
    71          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          byte[] b = getChecksum("SHA1", file);
    84          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          if (raw == null) {
    99              return null;
    100         }
    101         final StringBuilder hex = new StringBuilder(2 * raw.length);
    102         for (final byte b : raw) {
    103             hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
    104         }
    105         return hex.toString();
    106     }
    107 }