1 /*
2  * @(#) $Id: GenSRCommand.java,v 1.3 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.crypt;
11
12import java.util.*;
13import java.security.*;
14import java.io.*;
15import javax.crypto.SecretKey;
16import javax.crypto.KeyGenerator;
17
18import org.jstk.JSTKCommand;
19import org.jstk.JSTKCommandAdapter;
20import org.jstk.JSTKException;
21import org.jstk.JSTKArgs;
22import org.jstk.JSTKOptions;
23import org.jstk.JSTKUtil;
24import org.jstk.JSTKResult;
25
26public class GenSRCommand extends JSTKCommandAdapter{
27    private static HashMap defaults = new HashMap();
28    static {
29        defaults.put("algorithm", "SHA1PRNG");
30        defaults.put("size", "16");
31        defaults.put("seed", "1234567890");
32        defaults.put("num", "1");
33    }
34    public String briefDescription(){
35        return "generates a secret key ( for symmetric algorithms )";
36    }
37
38    public String[] useForms(){
39        String[] forms = {
40            "[-algorithm <alg>] [-size <size>] [-seed <seed>]\n" +
41            "\t[-num <num>] [-pprovider <provider>]\n"
42        };
43        return forms;
44    }
45
46    public String optionsDescription(){
47        return
48            "  -algorithm <alg>    : Algorithm for secure random generator.[" +
49            defaults.get("algorithm") + "]\n" +
50            "  -size <size>        : no. of random bytes to be generated.[" +
51            defaults.get("size") + "]\n" +
52            "  -seed <seed>        : seed to the random no. generator ( a long ).[" +
53            defaults.get("seed") + "]\n" +
54            "  -num <num>          : how many random nos. to generate.[" +
55            defaults.get("num") + "]\n" +
56            "  -provider <provider>: provider name for SecureRandom.\n";
57    }
58
59    public String[] sampleUses(){
60        String[] uses = {
61            "",
62            "-size 20",
63            "-seed 9876543210",
64            "-num 10"
65        };
66        return uses;
67    }
68    public Object execute(JSTKArgs args) throws JSTKException{
69        StringBuffer sb = new StringBuffer();
70        try {
71            args.setDefaults(defaults);
72            String providerName = args.get("provider");
73            String algorithm = args.get("algorithm");
74            String sizeString = args.get("size");
75            int size = Integer.parseInt(sizeString);
76            String seedString = args.get("seed");
77            long seed = Long.parseLong(seedString);
78            String numString = args.get("num");
79            int num = Integer.parseInt(numString);
80
81            SecureRandom sr = null;
82            if (providerName != null){
83                sr = SecureRandom.getInstance(algorithm, providerName);
84            } else {
85                sr = SecureRandom.getInstance(algorithm);
86            }
87            sr.setSeed(seed);
88            byte[] randomBytes = new byte[size];
89
90            sb.append("Generated Random Bytes: \n");
91            for (int i = 0; i < num; i++){
92                sr.nextBytes(randomBytes);
93                sb.append("[" + i + "]: " + JSTKUtil.hexStringFromBytes(randomBytes) + "\n");
94            }
95        } catch (Exception exc){
96            throw new JSTKException("GenKCommand.execute() failed", exc);
97        }
98        return new JSTKResult(null, true, sb.toString());
99    }
00
01    public static void main(String[] args) throws Exception {
02        JSTKOptions opts = new JSTKOptions();
03        opts.parse(args, 0);
04        GenSRCommand genSRCmd = new GenSRCommand();
05        JSTKResult result = (JSTKResult)genSRCmd.execute(opts);
06        System.out.println(result.getText());
07        System.exit(result.isSuccess()? 0 : 1);
08    }
09}