Skip to content

Commit 5d088d2

Browse files
Changes to security assignment
1 parent 5bf2b92 commit 5d088d2

File tree

2 files changed

+202
-100
lines changed

2 files changed

+202
-100
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Flex Panels 💪</title>
6+
<link href='https://fonts.googleapis.com/css?family=Amatic+SC' rel='stylesheet' type='text/css'>
7+
</head>
8+
<body>
9+
<style>
10+
html {
11+
box-sizing: border-box;
12+
background:#ffc600;
13+
font-family:'helvetica neue';
14+
font-size: 20px;
15+
font-weight: 200;
16+
}
17+
body {
18+
margin: 0;
19+
}
20+
*, *:before, *:after {
21+
box-sizing: inherit;
22+
}
23+
24+
.panels {
25+
min-height:100vh;
26+
overflow: hidden;
27+
}
28+
29+
.panel {
30+
background:#6B0F9C;
31+
box-shadow:inset 0 0 0 5px rgba(255,255,255,0.1);
32+
color:white;
33+
text-align: center;
34+
align-items:center;
35+
/* Safari transitionend event.propertyName === flex */
36+
/* Chrome + FF transitionend event.propertyName === flex-grow */
37+
transition:
38+
font-size 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
39+
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
40+
background 0.2s;
41+
font-size: 20px;
42+
background-size:cover;
43+
background-position:center;
44+
}
45+
46+
47+
.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
48+
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
49+
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
50+
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
51+
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }
52+
53+
.panel > * {
54+
margin:0;
55+
width: 100%;
56+
transition:transform 0.5s;
57+
}
58+
59+
.panel p {
60+
text-transform: uppercase;
61+
font-family: 'Amatic SC', cursive;
62+
text-shadow:0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
63+
font-size: 2em;
64+
}
65+
.panel p:nth-child(2) {
66+
font-size: 4em;
67+
}
68+
69+
.panel.open {
70+
font-size:40px;
71+
}
72+
73+
</style>
74+
75+
76+
<div class="panels">
77+
<div class="panel panel1">
78+
<p>Hey</p>
79+
<p>Let's</p>
80+
<p>Dance</p>
81+
</div>
82+
<div class="panel panel2">
83+
<p>Give</p>
84+
<p>Take</p>
85+
<p>Receive</p>
86+
</div>
87+
<div class="panel panel3">
88+
<p>Experience</p>
89+
<p>It</p>
90+
<p>Today</p>
91+
</div>
92+
<div class="panel panel4">
93+
<p>Give</p>
94+
<p>All</p>
95+
<p>You can</p>
96+
</div>
97+
<div class="panel panel5">
98+
<p>Life</p>
99+
<p>In</p>
100+
<p>Motion</p>
101+
</div>
102+
</div>
103+
104+
<script>
105+
106+
</script>
107+
108+
109+
110+
</body>
111+
</html>
Lines changed: 91 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import sun.plugin.dom.exception.InvalidStateException;
2+
3+
import java.io.IOException;
14
import java.nio.file.Files;
5+
import java.nio.file.Path;
26
import java.nio.file.Paths;
37
import java.util.Arrays;
48

@@ -9,56 +13,86 @@
913
* Description: Program that calculates a checksum based on bytes.
1014
* Every 1 byte for 8 bit, every two for 16 bit, and every 4 for 32 bit.
1115
*/
16+
public class Checksum {
1217

13-
public class checksum {
18+
private static final int[] VALID_BIT_SIZES = {8, 16, 32};
1419

1520
public static void main(String[] args) {
16-
int checkSumSize = 0, characterCount = 0, checkSumResult = 0;
17-
String input = null;
18-
byte[] fileBytes;
19-
20-
if (args.length < 2 || !Character.isDigit(args[1].charAt(0)) || !validBitSize(Integer.parseInt(args[1]))) {
21-
if (!Character.isDigit(args[1].charAt(0)) || !validBitSize(Integer.parseInt(args[1])))
22-
System.err.print("\nValid checksum sizes are 8, 16, or 32\n");
23-
else
24-
System.err.println("\nPlease provide the proper parameters.\n" +
25-
"First Parameter is the input file name, second is the size of the checksum.\n");
21+
if (args.length != 2) {
22+
System.err.println("\nPlease provide the proper parameters.\n" +
23+
"First Parameter is the input file name, second is the size of the checksum.\n");
24+
System.exit(1);
25+
}
26+
27+
Path filePath = Paths.get(args[0]);
28+
29+
if (!Files.exists(filePath)) {
30+
System.err.print("\nFile " + args[0] + " does not exist");
31+
System.exit(1);
32+
}
33+
34+
int checkSumSize;
35+
try {
36+
checkSumSize = Integer.parseInt(args[1]);
37+
} catch (NumberFormatException e) {
38+
System.err.print("\nValid checksum sizes are 8, 16, or 32\n");
2639
System.exit(1);
40+
return;
2741
}
2842

43+
if (Arrays.stream(VALID_BIT_SIZES).noneMatch(validBitSize -> validBitSize == checkSumSize)) {
44+
System.err.print("\nValid checksum sizes are 8, 16, or 32\n");
45+
System.exit(1);
46+
}
47+
48+
String input = null;
2949
try {
30-
input = readFileAsString(args[0]);
31-
} catch (Exception e) {
50+
input = new String(Files.readAllBytes(filePath));
51+
} catch (IOException e) {
3252
e.printStackTrace();
53+
System.exit(1);
54+
}
55+
56+
byte[] adjustedBytes = getAdjustedByteArray(input, checkSumSize);
57+
int checksum = checksum(adjustedBytes, checkSumSize);
58+
System.out.printf("\n%s\n%2d bit checksum is %8x for all %4d chars\n",
59+
formattedStringOutput(getAdjustedString(input, checkSumSize)), checkSumSize, checksum, adjustedBytes.length);
60+
61+
}
62+
63+
/**
64+
* @param in - input text to grab characters from to convert to byte.
65+
* @param bit - bit size we are using. bit > 8 ? sum every 2 chars : sum each chars
66+
* @return byte[] with appropriate padding if applicable. Padding with X (88 ASCII)
67+
*/
68+
private static byte[] getAdjustedByteArray(String in, int bit) {
69+
int originalSize = in.getBytes().length, newSize;
70+
71+
newSize = originalSize + getPadding(originalSize, bit);
72+
byte[] temp = new byte[newSize];
73+
74+
for (int i = 0; i < originalSize; i++) {
75+
temp[i] = (byte) in.charAt(i);
3376
}
3477

35-
if (input != null){
36-
switch (Integer.parseInt(args[1])) {
37-
case 8:
38-
checkSumSize = 8;
39-
fileBytes = getAdjustedByteArray(input, checkSumSize);
40-
characterCount = fileBytes.length;
41-
checkSumResult = checksum8(fileBytes);
42-
break;
43-
case 16:
44-
checkSumSize = 16;
45-
fileBytes = getAdjustedByteArray(input, checkSumSize);
46-
characterCount = fileBytes.length;
47-
checkSumResult = checksum16(fileBytes);
48-
break;
49-
case 32:
50-
checkSumSize = 32;
51-
fileBytes = getAdjustedByteArray(input, checkSumSize);
52-
characterCount = fileBytes.length;
53-
checkSumResult = checksum32(fileBytes);
54-
break;
55-
default:
56-
System.err.print("Valid checksum sizes are 8, 16, or 32\n");
57-
System.exit(1);
58-
break;
78+
if (getPadding(originalSize, bit) > 0) {
79+
for (int j = originalSize; j < newSize; j++) {
80+
temp[j] = 88;
5981
}
60-
System.out.printf("\n%s\n%2d bit checksum is %8x for all %4d chars\n",
61-
formattedStringOutput(getAdjustedString(input, checkSumSize)), checkSumSize, checkSumResult, characterCount);
82+
}
83+
return temp;
84+
}
85+
86+
private static int checksum(byte[] bytes, int numBits) {
87+
switch (numBits) {
88+
case 8:
89+
return checksum8(bytes);
90+
case 16:
91+
return checksum16(bytes);
92+
case 32:
93+
return checksum32(bytes);
94+
default:
95+
throw new InvalidStateException("Valid checksum sizes are 8, 16, or 32\n");
6296
}
6397
}
6498

@@ -71,8 +105,9 @@ public static void main(String[] args) {
71105
private static int checksum8(byte[] data) {
72106
int check = 0;
73107

74-
for (byte b : data)
108+
for (byte b : data) {
75109
check += b;
110+
}
76111

77112
return check & 0xFF;
78113
}
@@ -86,8 +121,9 @@ private static int checksum8(byte[] data) {
86121
private static int checksum16(byte[] data) {
87122
int check = 0;
88123

89-
for (int i = 0; i <= data.length - 2; i += 2)
124+
for (int i = 0; i <= data.length - 2; i += 2) {
90125
check += ((data[i] << 8) | (data[i + 1] & 0xFF));
126+
}
91127

92128
return check & 0xFFFF;
93129
}
@@ -101,23 +137,13 @@ private static int checksum16(byte[] data) {
101137
private static int checksum32(byte[] data) {
102138
int check = 0;
103139

104-
for (int i = 0; i < data.length; i += 4)
140+
for (int i = 0; i < data.length; i += 4) {
105141
check += ((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | (data[i + 3])) & 0xffffffffL;
142+
}
106143

107144
return check;
108145
}
109146

110-
/**
111-
* @param fileName - file to search
112-
* @return reads everything from the file and returns data in String format.
113-
* @throws Exception Citation: https://www.geeksforgeeks.org/different-ways-reading-text-file-java/
114-
*/
115-
private static String readFileAsString(String fileName) throws Exception {
116-
String data;
117-
data = new String(Files.readAllBytes(Paths.get(fileName)));
118-
return data;
119-
}
120-
121147
/**
122148
* @param output - message to format and output
123149
* @return formatted message output by adding a new line every 80 characters.
@@ -126,51 +152,29 @@ private static String readFileAsString(String fileName) throws Exception {
126152
private static String formattedStringOutput(String output) {
127153
StringBuilder res = new StringBuilder();
128154
for (int i = 0; i < output.length(); i++) {
129-
if (i > 0 && i % 80 == 0)
155+
if (i > 0 && i % 80 == 0) {
130156
res.append("\n");
157+
}
131158
res.append(output.charAt(i));
132159
}
133160
return res.toString();
134161
}
135162

136-
/**
137-
* @param in - input text to grab characters from to convert to byte.
138-
* @param bit - bit size we are using. bit > 8 ? sum every 2 chars : sum each chars
139-
* @return byte[] with appropriate padding if applicable. Padding with X (88 ASCII)
140-
*/
141-
private static byte[] getAdjustedByteArray(String in, int bit) {
142-
int originalSize = in.getBytes().length, newSize;
143-
144-
newSize = originalSize + getPadding(originalSize, bit);
145-
byte[] temp = new byte[newSize];
146-
147-
for (int i = 0; i < originalSize; i++) {
148-
temp[i] = (byte) in.charAt(i);
149-
}
150-
151-
if (getPadding(originalSize, bit) > 0) {
152-
for (int j = originalSize; j < newSize; j++) {
153-
temp[j] = 88;
154-
}
155-
}
156-
return temp;
157-
}
158-
159163
/**
160164
* @param in - input string to be adjusted
161165
* @param bit - bit size parameter to adjust text with padding character 'X'
162166
* @return formatted output
163167
*/
164168
private static String getAdjustedString(String in, int bit) {
165-
int originalSize = in.getBytes().length, newSize;
169+
int originalSize = in.getBytes().length, padding = getPadding(originalSize, bit), newSize;
166170
StringBuilder builder = new StringBuilder();
167-
newSize = originalSize + getPadding(originalSize, bit);
171+
newSize = originalSize + padding;
168172

169173
for (int i = 0; i < originalSize; i++) {
170174
builder.append(in.charAt(i));
171175
}
172176

173-
if (getPadding(originalSize, bit) > 0) {
177+
if (padding > 0) {
174178
for (int j = originalSize; j < newSize; j++) {
175179
builder.append("X");
176180
}
@@ -179,33 +183,20 @@ private static String getAdjustedString(String in, int bit) {
179183
return builder.toString();
180184
}
181185

182-
/**
183-
* @param bit - bit size parameter to check against the specified bit sizes that are compatible
184-
* @return boolean result if any match preset sizes
185-
*/
186-
private static boolean validBitSize(int bit) {
187-
int[] validBits = {8, 16, 32};
188-
return Arrays.stream(validBits).anyMatch(i -> i == bit);
189-
}
190-
191186

192187
/**
193188
* @param lengthOriginal - length of the input file non-padded
194189
* @param bit - bit size == 32 then we mod 4 else mod 2. (4 for every 4 bytes : 2 for every 2 bytes)
195-
* a - length
196-
* b - modulus
197-
* c - padding result
198190
* @return amount of padding we need to add to the original length (input length) if bit > 8
199191
*/
200192
private static int getPadding(int lengthOriginal, int bit) {
201-
int a = lengthOriginal;
202-
int b = bit == 32 ? 4 : 2;
203-
int c = 0;
204-
while (a % b != 0) {
205-
a = a + 1;
206-
c++;
193+
int length = lengthOriginal;
194+
int modulus = bit == 32 ? 4 : 2;
195+
int result = 0;
196+
while (length % modulus != 0) {
197+
length = length + 1;
198+
result++;
207199
}
208-
return bit > 8 ? c : 0;
200+
return bit > 8 ? result : 0;
209201
}
210-
211202
}

0 commit comments

Comments
 (0)