Jared Norris 6 anni fa
parent
commit
6580bb5198
5 ha cambiato i file con 66 aggiunte e 11 eliminazioni
  1. 5
    4
      Crypto/Crypto.iml
  2. 38
    4
      Crypto/src/ROT13.java
  3. 2
    3
      Crypto/src/ROT13Test.java
  4. 12
    0
      SimpleCrypt.iml
  5. 9
    0
      pom.xml

+ 5
- 4
Crypto/Crypto.iml Vedi File

@@ -7,12 +7,13 @@
7 7
     </content>
8 8
     <orderEntry type="inheritedJdk" />
9 9
     <orderEntry type="sourceFolder" forTests="false" />
10
-    <orderEntry type="library" name="Arquillian JUnit:Release" level="project" />
11 10
     <orderEntry type="module-library">
12
-      <library name="JUnit4">
11
+      <library name="JUnit5.0">
13 12
         <CLASSES>
14
-          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
15
-          <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
13
+          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
14
+          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
15
+          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
16
+          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
16 17
         </CLASSES>
17 18
         <JAVADOC />
18 19
         <SOURCES />

+ 38
- 4
Crypto/src/ROT13.java Vedi File

@@ -1,32 +1,66 @@
1 1
 import static java.lang.Character.isLowerCase;
2 2
 import static java.lang.Character.isUpperCase;
3 3
 import static java.lang.Character.toLowerCase;
4
+import java.util.*;
5
+import java.util.stream.*;
4 6
 
5 7
 public class ROT13  {
8
+    int shift;
9
+    Map<Character, Character> cypher;
10
+    String alphabet = "abcdefghijklmnopqrstuvwxyz";
6 11
 
7 12
     ROT13(Character cs, Character cf) {
13
+        shift = Math.abs((int)cs - (int)cf);
14
+        setupHM();
8 15
     }
9 16
 
10 17
     ROT13() {
18
+        shift = 13;
19
+        setupHM();
20
+    }
21
+
22
+    public void setupHM() {
23
+        cypher = new HashMap<>();
24
+        for(Character c : alphabet.toCharArray()) {
25
+            int offset = (int)c + shift > 122 ? (int)c - shift : (int)c  + shift;
26
+            cypher.put(c, (char)offset);
27
+        }
11 28
     }
12 29
 
13 30
 
14 31
     public String crypt(String text) throws UnsupportedOperationException {
32
+        StringBuilder result = new StringBuilder();
15 33
 
16
-        return "";
34
+        for(Character c : text.toCharArray()) {
35
+            if (Character.isLetter(c)) {
36
+                if (Character.isUpperCase(c)) {
37
+                    result.append(Character.toUpperCase(cypher.get(Character.toLowerCase(c))));
38
+                }
39
+                else result.append(cypher.get(c));
40
+            }
41
+            else {
42
+                result.append(c);
43
+            }
44
+        }
45
+        return result.toString();
17 46
     }
18 47
 
19 48
     public String encrypt(String text) {
20
-        return text;
49
+        return crypt(text);
21 50
     }
22 51
 
23 52
     public String decrypt(String text) {
24
-        return text;
53
+        return crypt(text);
25 54
     }
26 55
 
27 56
     public static String rotate(String s, Character c) {
57
+        String[] rotator = s.split("");
58
+        StringBuilder result = new StringBuilder();
28 59
 
29
-        return "";
60
+        for(int i = 0; i < rotator.length; i++) {
61
+            result.append(rotator[(s.indexOf(c) + i) % rotator.length]);
62
+        }
63
+        return result.toString();
30 64
     }
31 65
 
32 66
 }

+ 2
- 3
Crypto/src/ROT13Test.java Vedi File

@@ -1,6 +1,5 @@
1
-import org.junit.Test;
2
-
3
-import static org.junit.Assert.*;
1
+import static org.junit.jupiter.api.Assertions.*;
2
+import org.junit.jupiter.api.Test;
4 3
 
5 4
 public class ROT13Test {
6 5
 

+ 12
- 0
SimpleCrypt.iml Vedi File

@@ -0,0 +1,12 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4
+    <output url="file://$MODULE_DIR$/target/classes" />
5
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
+    <content url="file://$MODULE_DIR$">
7
+      <excludeFolder url="file://$MODULE_DIR$/target" />
8
+    </content>
9
+    <orderEntry type="inheritedJdk" />
10
+    <orderEntry type="sourceFolder" forTests="false" />
11
+  </component>
12
+</module>

+ 9
- 0
pom.xml Vedi File

@@ -8,5 +8,14 @@
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.8.1</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+    </dependencies>
19
+
11 20
 
12 21
 </project>