From 3aa70da27033412b3310fb393c924d1eaa5cf022 Mon Sep 17 00:00:00 2001 From: Jeremy Jones Date: Fri, 26 Sep 2014 18:10:05 -0700 Subject: [PATCH] first commit --- .gitignore | 2 + Makefile | 35 +++++ append.java | 266 ++++++++++++++++++++++++++++++++++ batch | 5 + encryptionTest/StrongAES.java | 38 +++++ readme.txt | 1 + 6 files changed, 347 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 append.java create mode 100644 batch create mode 100644 encryptionTest/StrongAES.java create mode 100644 readme.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab31b20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.class + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b76714e --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# +# A simple makefile for compiling three java classes +# + +# define a makefile variable for the java compiler +# +JCC = javac + +# define a makefile variable for compilation flags +# the -g flag compiles with debugging information +# +JFLAGS = -g + +# typing 'make' will invoke the first target entry in the makefile +# (the default one in this case) +# +default: poop +# this target entry builds the Average class +# the Average.class file is dependent on the Average.java file +# and the rule associated with this entry gives the command to create it +# +poop: append.java + $(JCC) *.java + +#Convert.class: Convert.java +# $(JCC) $(JFLAGS) Convert.java + +#Volume.class: Volume.java +# $(JCC) $(JFLAGS) Volume.java + +# To start over from scratch, type 'make clean'. +# Removes all .class files, so that the next make rebuilds them +# +clean: + $(RM) *.class diff --git a/append.java b/append.java new file mode 100644 index 0000000..e627ff4 --- /dev/null +++ b/append.java @@ -0,0 +1,266 @@ +import java.util.*; +import java.io.*; + +public class append +{ + public static Scanner in = new Scanner(System.in); + public static void main(String[] args) { + + String [] logArray = args; + + if (logArray[0].equals("-B")) { + BatchReader reader = new BatchReader(); + LogAppender logTester = new LogAppender(); + + if (reader.openFile(logArray[1]) == true) { + reader.readFile(logTester); + reader.closeFile(); + } + } + else + System.out.println("poop"); //read single logs + + } +} + +class BatchReader +{ + private Scanner x; + + public boolean openFile(String dataFile) + { + try{ + x = new Scanner(new File(dataFile)); + } + catch(Exception e){ + System.out.println("could not read file."); + return false; + } + return true; + } + + public void readFile(LogAppender logger) + { + String firstline = x.nextLine(); + //System.out.println(firstline); + logger.validateLog(firstline); + + //open log based on log name and pw of first line + //logger.openLog(firstline); + //if openLog was successful then go to while else error + + while(x.hasNextLine()) + { + String logLine = x.nextLine(); + logger.validateLog(logLine); + //read in individual log lines + } + } + + public void closeFile() + { x.close(); } +} + +class LogAppender +{ + private String currentPassword = null; + private String encryptionKey = "blablad93j2wp0s1"; + private String currentLog = null; + + //make a hashtable of persons with name+log as the key + //private ArrayList currentPersons = new ArrayList<>(); + + public void validateLog(String log) + { + String password, name, logName; + int time, room; + boolean arriving, guest, inLobby; + + System.out.println(log); + StringTokenizer check = new StringTokenizer(log); + + // check for valid length + int logLength = check.countTokens(); + if (logLength == 8) + inLobby = true; + else if (logLength == 10) + inLobby = false; + else + { + System.out.println("Invalid length of log entry"); + return; + } + + // check for -K + if (check.nextToken().equals("-K") == false) { + System.out.println("no -K command"); + return; + } + + // check password + password = check.nextToken(); + if (password.matches("\\A\\p{Alnum}*\\z") == false) { + System.out.println("incorrect password (must be alphanumeric)"); + return; + } + + // check -T + if (check.nextToken().equals("-T") == false) { + System.out.println("no -T command"); + return; + } + + // check time + String timeEntry = check.nextToken(); + if (timeEntry.matches("\\A\\p{Digit}*\\z") == false) { + System.out.println("invalid time entry"); + return; + } + else + time = Integer.parseInt(timeEntry); + + // check -A or -L + String movement = check.nextToken(); + if (movement.equals("-A")) + arriving = true; + else if (movement.equals("-L")) + arriving = false; + else + System.out.println("invalid movement"); + + // check if entry has 5 or 3 more elements + if (check.countTokens() == 5) { + + // check -R + if (check.nextToken().equals("-R") == false) { + System.out.println("invalid room command"); + return; + } + + // check room + String roomEntry = check.nextToken(); + if (roomEntry.matches("\\A\\p{Digit}*\\z") == false) { + System.out.println("invalid room number"); + return; + } + else + room = Integer.parseInt(roomEntry); + } + + // check -G or -E + String personType = check.nextToken(); + if (personType.equals("-G")) + guest = true; + else if (personType.equals("-E")) + guest = false; + else + System.out.println("invalid personType command"); + + // check name + name = check.nextToken(); + if (name.matches("\\A\\p{Alpha}*\\z") == false) { + System.out.println("Invalid name (must be alphabetic"); + return; + } + + logName = check.nextToken(); + if (logName.matches("\\A\\p{Alnum}*\\z") == false) { + System.out.println("invalid log name (must be alphanumeric)"); + return; + } + + //check logname and verify password + //if no log exists and inLobby = true, then create new log + } + +} + +class LogFile +{ + private String logName; + private int currentTime; + private String password; + private String key; + + Hashtable log = new Hashtable<>(); + + public LogFile(String logName) + { + this.logName = logName; + } + + public void addPerson(Person newP) + { + log.put(newP.getName(), newP); + } + + public String getLogName() + { return logName; } + + public Person getPerson(String name) + { + return log.get(name); + } + + public boolean varifyPassword(String testPassword) + { + return password.equals(testPassword); + } + + +} + +class Person +{ + public Person(String name, String type, int location, + String movement, int time, String logName, String listOfRooms) + { + this.time = time; //-T 1 + this.movement = movement; //-A + this.type = type; //-E + this.name = name; //Joe + this.location = location; //-R 1 + this.logName = logName; //log1 + this.listOfRooms = listOfRooms; + } + + public String getName() + { return name; } + + public boolean hasLeftLastRoom() + { + if (movement.equals("-L")) + return true; + return false; + } + + public int checkTime() + { return time; } + + public void updateMovement(int location, String movement, int time) + { + this.location = location; + this.movement = movement; + this.time = time; + listOfRooms.concat(", " + location); + } + + public String toString() + { + return name + type + location + movement + time + logName + listOfRooms; + } + + private int time; + private String movement; + private String type; + private String name; + private int location; + private String logName; + private String listOfRooms; +} + + + + + + diff --git a/batch b/batch new file mode 100644 index 0000000..e6bd62d --- /dev/null +++ b/batch @@ -0,0 +1,5 @@ +-K secret -T 0 -A -E John log2 +-K secret -T 1 -A -R 0 -E John log2 +-K secret -T 2 -A -G James log2 +-K secret -T 3 -A -R 0 -G James log2 +-K secret -T 5 -L -R 0 -G James log2 \ No newline at end of file diff --git a/encryptionTest/StrongAES.java b/encryptionTest/StrongAES.java new file mode 100644 index 0000000..f6dd232 --- /dev/null +++ b/encryptionTest/StrongAES.java @@ -0,0 +1,38 @@ +//package com.example; + +import java.security.Key; +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; + +public class StrongAES { + + public void run() { + try { + String text = "Hello World" + "\nNew Line"; + //String key = "Bar12345Bar12345"; // 128 bit key + String key = "blablad93j2wp0s1"; + + // Create key and cipher + Key aesKey = new SecretKeySpec(key.getBytes(), "AES"); + System.out.println("key: " + aesKey); + Cipher cipher = Cipher.getInstance("AES"); + + // encrypt the text + cipher.init(Cipher.ENCRYPT_MODE, aesKey); + byte[] encrypted = cipher.doFinal(text.getBytes()); + System.err.println(new String(encrypted)); + + // decrypt the text + cipher.init(Cipher.DECRYPT_MODE, aesKey); + String decrypted = new String(cipher.doFinal(encrypted)); + System.err.println(decrypted); + }catch(Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + StrongAES app = new StrongAES(); + app.run(); + } +} diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..331cea5 --- /dev/null +++ b/readme.txt @@ -0,0 +1 @@ +Program for the Buil-it Break-it Fix-it competition