Programm besser strukturiert. Dabei ist aufgefallen, dass die Bedingungen für ein...
authorStephan Richter <github@keawe.de>
Mon, 11 Sep 2017 08:12:25 +0000 (10:12 +0200)
committerStephan Richter <github@keawe.de>
Mon, 11 Sep 2017 08:12:25 +0000 (10:12 +0200)
src/ScharedSecret.java

index f38cbe3d860fa42e830e60373bde48592e90e3c0..0dd5fc7b44efd775d19c4b4f145ec547fd61e717 100644 (file)
@@ -1,3 +1,4 @@
+import java.io.IOException;
 import java.sql.Time;
 import java.util.Random;
 
@@ -100,35 +101,46 @@ public class ScharedSecret {
                return result;
        }
        
+       private static String secret(String[] args){
+               if (args != null && args.length > 0) return args[0];            
+               
+               try {
+                       String secret = "";
+                       System.out.println("Please enter your secret:");
+                       int c;
+                       while ((c = System.in.read()) != 10){
+                               secret+=(char)c;
+                       }
+                       return secret;
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       System.exit(1);                 
+               }
+               return null;
+       }
+       
        public static void main(String[] args) {
-               String secret="gnome-keyring";
-               String code1="";
-               String code2="";
-               String code3="";
-               String code4="";
-               String code5="";
+               String secret = secret(args);
+               String [] codes = { "", "", "", "", "" };
                System.out.print("  Binary: ");
                for (int i=0; i<secret.length();i++){
-                       String[] codes = encodeChar(secret.charAt(i));
-                       code1+=codes[0]+" ";
-                       code2+=codes[1]+" ";
-                       code3+=codes[2]+" ";
-                       code4+=codes[3]+" ";
-                       code5+=codes[4]+" ";
+                       String[] chars = encodeChar(secret.charAt(i));
+                       for (int j = 0; j<5; j++) codes[j] += chars[j]+" ";
                }
                System.out.println();
-               System.out.println("\nSecret 1: "+code1);
-               System.out.println("\nSecret 2: "+code2);
-               System.out.println("\nSecret 3: "+code3);
-               System.out.println("\nSecret 4: "+code4);
-               System.out.println("\nSecret 5: "+code5);
+               
+               for (int j=0; j<5; j++) System.out.println("\nSecret "+j+": "+codes[j]);
                System.out.print("\nChecksum: ");
-               for (int i=0; i<code1.length(); i++){
+               for (int i=0; i<codes[0].length(); i++){
                        
                        if (i%9==8) {
                                System.out.print(" ");
                        } else {
-                               System.out.print((code1.charAt(i)+code2.charAt(i)+code3.charAt(i)+code4.charAt(i)+code5.charAt(i))>(240+22)?1:0);
+                               System.out.print((codes[0].charAt(i)
+                                                                                                +codes[1].charAt(i)
+                                                                                                +codes[2].charAt(i)
+                                                                                                +codes[3].charAt(i)
+                                                                                                +codes[4].charAt(i))>(240+22)?1:0);
                        }
                }
        }