19 package org.sleuthkit.autopsy.modules.encryptiondetection;
 
   21 import java.io.BufferedInputStream;
 
   22 import java.io.IOException;
 
   23 import java.io.InputStream;
 
   24 import org.openide.util.NbBundle;
 
   32 final class EncryptionDetectionTools {
 
   34     private static final double ONE_OVER_LOG2 = 1.4426950408889634073599246810019; 
 
   35     private static final int BYTE_OCCURENCES_BUFFER_SIZE = 256;
 
   36     static final double MINIMUM_ENTROPY_INPUT_RANGE_MIN = 6.0;
 
   37     static final double MINIMUM_ENTROPY_INPUT_RANGE_MAX = 8.0;
 
   38     static final int MINIMUM_FILE_SIZE_INPUT_RANGE_MIN = 1;
 
   41         "EncryptionDetectionTools.errorMessage.minimumEntropyInput=Minimum entropy input must be a number between 6.0 and 8.0." 
   47     static void validateMinEntropyValue(
double minimumEntropy) 
throws IngestModule.IngestModuleException {
 
   48         if (minimumEntropy < MINIMUM_ENTROPY_INPUT_RANGE_MIN || minimumEntropy > MINIMUM_ENTROPY_INPUT_RANGE_MAX) {
 
   49             throw new IngestModule.IngestModuleException(Bundle.EncryptionDetectionTools_errorMessage_minimumEntropyInput());
 
   54         "EncryptionDetectionTools.errorMessage.minimumFileSizeInput=Minimum file size input must be an integer (in megabytes) of 1 or greater." 
   60     static void validateMinFileSizeValue(
int minimumFileSize) 
throws IngestModule.IngestModuleException {
 
   61         if (minimumFileSize < MINIMUM_FILE_SIZE_INPUT_RANGE_MIN) {
 
   62             throw new IngestModule.IngestModuleException(Bundle.EncryptionDetectionTools_errorMessage_minimumFileSizeInput());
 
   80     static double calculateEntropy(Content content) 
throws ReadContentInputStream.ReadContentInputStreamException, IOException {
 
   86         InputStream in = null;
 
   87         BufferedInputStream bin = null;
 
   90             in = 
new ReadContentInputStream(content);
 
   91             bin = 
new BufferedInputStream(in);
 
   96             int[] byteOccurences = 
new int[BYTE_OCCURENCES_BUFFER_SIZE];
 
   98             while ((readByte = bin.read()) != -1) {
 
   99                 byteOccurences[readByte]++;
 
  105             long dataLength = content.getSize() - 1;
 
  106             double entropyAccumulator = 0;
 
  107             for (
int i = 0; i < BYTE_OCCURENCES_BUFFER_SIZE; i++) {
 
  108                 if (byteOccurences[i] > 0) {
 
  109                     double byteProbability = (double) byteOccurences[i] / (
double) dataLength;
 
  110                     entropyAccumulator += (byteProbability * Math.log(byteProbability) * ONE_OVER_LOG2);
 
  114             return -entropyAccumulator;
 
  129     private EncryptionDetectionTools() {