Luninariel

Entropy

Oct 10th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. package edu.missouriwestern.noynaert.csc254;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * This program calculates entropy of passwords, and checks to see if passwords
  9.  * are on a list of common passwords.
  10.  *
  11.  * @author YOUR NAME
  12.  * @since 2018/10/09
  13.  */
  14. public class Entropy {
  15.     static final String AUTHOR = "YOUR NAME";
  16.  
  17.     public static void main(String[] args) {
  18.         String fileName = getFileNameFromArgs(args);
  19.         int n = 0;
  20.         try {
  21.             Scanner input = new Scanner(new File(fileName));
  22.             System.out.printf("%8s %8s %8s %8s %8s %8s %s %s\n",
  23.                     "Length", "Upper", "Lower", "Digits", "Symbols", "Range", "Entropy", "Pass Phrase");
  24.             while(input.hasNextLine()) {
  25.                 String line = input.nextLine().trim();
  26.                 String symbols = "`~!@#$%^&*()-_=+[{\\]}|;:'\",<.>/?";
  27.                 boolean hasUpper = hasUpper(line);
  28.                 boolean hasLower = hasLower(line);
  29.                 boolean hasDigit = hasDigit(line);
  30.                 int length = line.length();
  31.                 int countSymbols = countSymbols(line,symbols);
  32.                 int range=range(line,symbols);
  33.                 int entropy = calculateEntropy(range,line);
  34.                 System.out.printf("%8d %8b %8b %8b %8d %8d %8d %s\n",
  35.                         length, hasUpper, hasLower, hasDigit, countSymbols, range,entropy, line);
  36.                 n++;
  37.             }
  38.             input.close();
  39.         } catch (FileNotFoundException e) {
  40.             System.err.printf("The file \"%s\" does not exist.", fileName);
  41.             System.exit(1);
  42.         }
  43.  
  44.         System.out.printf("\nRecords processed: %d\n", n);
  45.         System.out.println("Programmed by " + AUTHOR);
  46.     }
  47.     public static int calculateEntropy(int range, String word){
  48.         //e=log2(r^(L-1)
  49.         double power=word.length()-1;
  50.         double tobeLogged=Math.pow((double)range, power);
  51.         double e=Math.log(tobeLogged)/Math.log(2.);
  52.         return (int)e;
  53.  
  54.  
  55.     }
  56.     public static int countSymbols(String word, String symbols){
  57.         int result = 0;
  58.         for(int i=0; i<symbols.length();i++)
  59.             if(containsSymbol(word, symbols.charAt(i)))
  60.                 result++;
  61.             return result;
  62.     }
  63.     public static boolean containsSymbol(String word, char symbol){
  64.         boolean result = (word.indexOf(symbol)>-1);
  65.         return result;
  66.  
  67.     }
  68.  
  69.     public static boolean hasDigit(String word){
  70.         boolean result = word.matches(".*[0-9].*");
  71.         return result;
  72.     }
  73.     public static boolean hasLower(String word){
  74.         boolean result = word.matches(".*[a-z].*");
  75.         return result;
  76.     }
  77.     public static boolean hasUpper(String word) {
  78.         boolean result = word.matches(".*[A-Z].*");
  79.         return result;
  80.     }
  81.  
  82.     /**
  83.      * If the args array is empty, then the file name is "input.txt"
  84.      * If the args array is not empty, then the file name is set to args[0]
  85.      *
  86.      * @param args  An array of strings.  Normally this will be args from the main(String[] args) method.
  87.      * @return A potential file name.  A file with the corresponding name is not guaranteed to exist.
  88.      */
  89.     public static String getFileNameFromArgs(String[] args) {
  90.         String fileName = (args.length==0) ? "input.txt" : args[0];
  91.         return fileName;
  92.     }
  93.  
  94.     /**
  95.      * Calculates the base 10 log of a number
  96.      *
  97.      * @param x  A real number.  The number should be positive
  98.      * @return   The log<sub>10</sub> value of x
  99.      */
  100.     public static double log10(double x) {
  101.         double result = Math.log(x) / Math.log(10);
  102.         return result;
  103.     }
  104.  
  105.     /**
  106.      * @TODO Fill this in
  107.      *
  108.      * @param x
  109.      * @return
  110.      */
  111.     public static double log2(double x){
  112.         double result = Math.log(x) / Math.log(2.);
  113.         return result;
  114.     }
  115.  
  116.     /**
  117.      * Calculates the range of a string.  The string s is scanned.
  118.      * The range is initially 0.
  119.      * <pre>
  120.      *    Add 26 to the range if the string contains at least 1 upper case letter
  121.      *    Add 26 to the range if the string contains at least 1 lower case letter
  122.      *    Add 10 to the range if the string contains at least 1 digit
  123.      *    Add 1 to the range for each of the special characters in
  124.      *            "`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?</.>"
  125.      * </pre>
  126.  
  127.      */
  128.     public static int range(String word, String symbols){
  129.         int result = 0;
  130.         if(hasUpper(word))
  131.              result += 26;
  132.         if(hasLower(word))
  133.             result += 26;
  134.         if(hasDigit(word))
  135.             result +=10;
  136.  
  137.         result +=countSymbols(word,symbols);
  138.  
  139.         return result;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment