1
10package org.jstk.crypt;
11
12import java.util.*;
13import java.security.*;
14
15
16import org.jstk.*;
17
18
19public class CryptTool{
20 static HashMap cmds = new HashMap(); static {
22 cmds.put("listp", new ListPCommand());
23 cmds.put("genk", new GenKCommand());
24 cmds.put("genkp", new GenKPCommand());
25 cmds.put("gensr", new GenSRCommand());
26 cmds.put("listks", new ListKSCommand());
27 cmds.put("export", new ExportCommand());
28 cmds.put("crypt", new CryptCommand());
29 cmds.put("digest", new DigestCommand());
30 cmds.put("mac", new MacCommand());
31 cmds.put("sign", new SignCommand());
32 cmds.put("bench", new BenchCommand());
33 }
34 public String progName(){
35 String progName = System.getProperty("org.jstk.crypt.progname");
36 if (progName == null)
37 progName = "java org.jstk.crypt.CryptTool";
38
39 return progName;
40 }
41 public String briefDescription(){
42 return "performs cryptographic operations";
43 }
44 public String extendedUsageString(){
45 StringBuffer sb = new StringBuffer();
46 Iterator itr = cmds.entrySet().iterator();
47 while (itr.hasNext()){
48 Map.Entry ent = (Map.Entry)itr.next();
49 String key = (String)ent.getKey();
50 JSTKCommand cmd = (JSTKCommand)ent.getValue();
51
52 sb.append(" " + key);
53 int blanksNeeded = 12 - key.length();
54 for (int i = 0; i < blanksNeeded; i++)
55 sb.append(" ");
56 sb.append(cmd.briefDescription() + "\n");
57 }
58 return sb.toString();
59 }
60
61 public void printUsage(){
62 System.out.println("Description:: \n " + briefDescription());
63 System.out.println("Usage:: \n " + progName() + " <cmd> [<options>]");
64 System.out.println("Commands:: ");
65 System.out.println(extendedUsageString());
66 System.out.println("Notes:: ");
67 System.out.println(" -- Type \"" + progName() + " <cmd> help\" to get command specific help.");
68 System.out.println(" -- Specify option \"-showtime\" to get command execution time. Example:");
69 System.out.println(" " + progName() + " genk -showtime");
70 }
71
72 public void printCmdUsage(JSTKCommand cmd, String cmdString){
73 System.out.println("Description:: \n " + cmd.briefDescription());
74 System.out.println("\nUsage:: \n " + progName() + " " + cmdString + " [<options>]\n");
75
76 String[] forms = cmd.useForms();
77 if (forms != null){
78 for (int i = 0; i < forms.length; i++){
79 System.out.println(" " + progName() + " " + cmdString + " " + forms[i]);
80 }
81 }
82
83 System.out.println("\nOptions:: ");
84 System.out.println(cmd.optionsDescription());
85 String[] uses = cmd.sampleUses();
86 if (uses != null){
87 System.out.println("Sample Uses:: ");
88 for (int i = 0; i < uses.length; i++){
89 System.out.println(" " + progName() + " " + cmdString + " " + uses[i]);
90 }
91 }
92 }
93
94 public static void main(String[] args) throws Exception {
95 CryptTool ct = new CryptTool();
96 JSTKOptions opts = new JSTKOptions();
97 if (args.length < 1){ ct.printUsage();
99 return;
00 }
01 String cmdString = args[0];
02 if (cmdString.equals("-h") || cmdString.equals("help") || cmdString.equals("-?")){
03 ct.printUsage();
04 return;
05 }
06
07 JSTKCommand cmd = (JSTKCommand)cmds.get(cmdString);
08 if (cmd == null){ System.out.println("Unknown Command: " + cmdString);
10 ct.printUsage();
11 return;
12 }
13
14 if (args.length > 1 && (args[1].equals("-h") || args[1].equals("help") || args[1].equals("-?"))){
15 ct.printCmdUsage(cmd, cmdString);
16 return;
17 }
18
19 opts.parse(args, 1);
20 boolean showtime = Boolean.valueOf(opts.get("showtime")).booleanValue();
21 long ts = 0, tt = 0;
22 if (showtime)
23 ts = System.currentTimeMillis();
24
25 JSTKResult result = (JSTKResult)cmd.execute(opts);
26
27 if (showtime)
28 tt = System.currentTimeMillis() - ts;
29 System.out.println(result.getText());
30 if (showtime)
31 System.out.println("Execution Time: " + (float)tt/1000.0 + " secs.");
32 System.exit(result.isSuccess() ? 0 : 1);
33 }
34}