1
10package org.jstk.crypt;
11
12import java.util.HashMap;
13import java.io.File;
14import java.security.MessageDigest;
15
16import org.jstk.*;
17
18public class DigestCommand extends JSTKCommandAdapter {
19 private static HashMap defaults = new HashMap();
20 static {
21 defaults.put("algorithm", "SHA");
22 }
23
24 public String briefDescription(){
25 String briefDesc = "creates or verifies message digest";
26 return briefDesc;
27 }
28
29 public String optionsDescription(){
30 String optionsDesc =
31 " -verify : match the digest of the infile with given digest.\n" +
32 " -infile <infile> : message file.\n" +
33 " -mdfile <mdfile> : message digest file.\n" +
34 " -mdbytes <mdbytes> : message digest bytes in hexadecimal.\n" +
35 " -algorithm <alg> : algorithm for message digest computation.[" +
36 defaults.get("algorithm") + "]\n" +
37 " -provider <provider>: provider name for MessageDigest.\n";
38 return optionsDesc;
39 }
40 public String[] useForms(){
41 String[] useForms = {
42 "-infile <infile> [-mdfile <mdfile>]\n" +
43 "\t[-algorithm <alg>] [-provider <provider>]",
44 "-verify -infile <infile> (-mdfile <dfile> | -mdbytes\n" +
45 "\t<mdbytes>) [-algorithm <alg>] [-provider <provider>]"
46 };
47 return useForms;
48 }
49 public String[] sampleUses(){
50 String[] sampleUses = {
51 "-infile test.txt",
52 "-infile test.txt -mdfile test.md",
53 "-verify -infile test.txt -mdfile test.md",
54 "-verify -infile test.txt -mdbytes <...>"
55 };
56 return sampleUses;
57 }
58
59
60 public Object execute(JSTKArgs args) throws JSTKException{
61 try {
62 args.setDefaults(defaults);
63
64 String providerName = (String)args.get("provider");
65 String algorithm = args.get("algorithm");
66 boolean verify = Boolean.valueOf((String)args.get("verify")).booleanValue();
67 boolean stream = Boolean.valueOf((String)args.get("stream")).booleanValue();
68 String mdString = args.get("mdbytes");
69 String infile = args.get("infile");
70 String mdfile = args.get("mdfile");
71
72 if (infile == null)
74 return new JSTKResult(null, false, "no message file specified");
75
76 byte[] mdbytesV = null;
78 if (verify){
79 if (mdString != null && mdfile != null)
80 return new JSTKResult(null, false, "too many digests to verify against");
81
82 if (mdString != null){
83 mdbytesV = JSTKUtil.bytesFromHexString(mdString);
84 } else if (mdfile != null){
85 mdbytesV = JSTKUtil.bytesFromFile(mdfile);
86 } else {
87 return new JSTKResult(null, false, "no digest to verify against");
88 }
89 }
90
91 MessageDigest md = null;
92 if (providerName != null)
93 md = MessageDigest.getInstance(algorithm, providerName);
94 else
95 md = MessageDigest.getInstance(algorithm);
96
97 byte[] bytes = JSTKUtil.bytesFromFile(infile);
98 md.update(bytes);
99 byte[] mdbytes = md.digest();
00 if (verify){
01 if (MessageDigest.isEqual(mdbytes, mdbytesV))
02 return new JSTKResult(Boolean.TRUE, true, "verification succeeded");
03 else
04 return new JSTKResult(Boolean.FALSE, true, "verification failed");
05 } else {
06 if (mdfile != null){
07 JSTKUtil.bytesToFile(mdbytes, mdfile);
08 return new JSTKResult(mdbytes, true, "digest written to file: " + mdfile);
09 } else {
10 String hexString = JSTKUtil.hexStringFromBytes(mdbytes);
11 return new JSTKResult(mdbytes, true, "Message Digest (Hex)::\n" + hexString);
12 }
13 }
14 } catch (Exception exc){
15 throw new JSTKException("DigestCommand execution failed", exc);
16 }
17 }
18
19 public static void main(String[] args) throws Exception {
20 JSTKOptions opts = new JSTKOptions();
21 opts.parse(args, 0);
22 DigestCommand digestCmd = new DigestCommand();
23 JSTKResult result = (JSTKResult)digestCmd.execute(opts);
24 System.out.println(result.getText());
25 System.exit(result.isSuccess()? 0 : 1);
26 }
27}