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;
 
   33 final class EncryptionDetectionTools {
 
   35     private static final double ONE_OVER_LOG2 = 1.4426950408889634073599246810019; 
 
   36     private static final int BYTE_OCCURENCES_BUFFER_SIZE = 256;
 
   37     static final double MINIMUM_ENTROPY_INPUT_RANGE_MIN = 6.0;
 
   38     static final double MINIMUM_ENTROPY_INPUT_RANGE_MAX = 8.0;
 
   39     static final int MINIMUM_FILE_SIZE_INPUT_RANGE_MIN = 1;
 
   42         "EncryptionDetectionTools.errorMessage.minimumEntropyInput=Minimum entropy input must be a number between 6.0 and 8.0." 
   48     static void validateMinEntropyValue(
double minimumEntropy) 
throws IngestModule.IngestModuleException {
 
   49         if (minimumEntropy < MINIMUM_ENTROPY_INPUT_RANGE_MIN || minimumEntropy > MINIMUM_ENTROPY_INPUT_RANGE_MAX) {
 
   50             throw new IngestModule.IngestModuleException(Bundle.EncryptionDetectionTools_errorMessage_minimumEntropyInput());
 
   55         "EncryptionDetectionTools.errorMessage.minimumFileSizeInput=Minimum file size input must be an integer (in megabytes) of 1 or greater." 
   61     static void validateMinFileSizeValue(
int minimumFileSize) 
throws IngestModule.IngestModuleException {
 
   62         if (minimumFileSize < MINIMUM_FILE_SIZE_INPUT_RANGE_MIN) {
 
   63             throw new IngestModule.IngestModuleException(Bundle.EncryptionDetectionTools_errorMessage_minimumFileSizeInput());
 
   82     static double calculateEntropy(Content content, IngestJobContext context) 
throws ReadContentInputStream.ReadContentInputStreamException, IOException {
 
   88         InputStream in = null;
 
   89         BufferedInputStream bin = null;
 
   92             in = 
new ReadContentInputStream(content);
 
   93             bin = 
new BufferedInputStream(in);
 
   98             int[] byteOccurences = 
new int[BYTE_OCCURENCES_BUFFER_SIZE];
 
  101             while ((readByte = bin.read()) != -1) {
 
  102                 byteOccurences[readByte]++;
 
  106                 if (bytesRead % 10000 == 0) {
 
  107                     if (context.dataSourceIngestIsCancelled() || context.fileIngestIsCancelled()) {
 
  116             long dataLength = content.getSize() - 1;
 
  117             double entropyAccumulator = 0;
 
  118             for (
int i = 0; i < BYTE_OCCURENCES_BUFFER_SIZE; i++) {
 
  119                 if (byteOccurences[i] > 0) {
 
  120                     double byteProbability = (double) byteOccurences[i] / (
double) dataLength;
 
  121                     entropyAccumulator += (byteProbability * Math.log(byteProbability) * ONE_OVER_LOG2);
 
  125             return -entropyAccumulator;
 
  140     private EncryptionDetectionTools() {