KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DummySignatureHandler


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.util.zip.CRC32 JavaDoc;
4 import java.security.KeyStore JavaDoc;
5 import java.security.GeneralSecurityException JavaDoc;
6
7 // This simple class shows how to create a SignatureHandler class
8
// that verifies the document using a CRC-32 checksum.
9
//
10
public class DummySignatureHandler extends SignatureHandler
11 {
12     public DummySignatureHandler()
13     {
14         super();
15     }
16
17     public String JavaDoc getFilter()
18     {
19         return "Dummy.CRC32";
20     }
21
22     protected void prepareToSign(KeyStore JavaDoc store, String JavaDoc alias, char[] password)
23         throws GeneralSecurityException JavaDoc
24     {
25         // not using a keystore in this example, so just ignore them.
26
super.prepareToSign(store, alias, password);
27         putNumericValue("Version", 1); // Add some values to the signature dictionary
28
}
29
30     public byte[] sign(InputStream in)
31     {
32         CRC32 JavaDoc crc = new CRC32 JavaDoc();
33         int b;
34     try {
35         while ((b=in.read())>=0) {
36         crc.update(b);
37         }
38     } catch (IOException e) {
39         throw new RuntimeException JavaDoc("Unexpected IOException!");
40     }
41     int val = (int)crc.getValue();
42         byte[] out = new byte[4];
43         out[0]=(byte)(val>>24);
44         out[1]=(byte)(val>>16);
45         out[2]=(byte)(val>>8);
46         out[3]=(byte)(val);
47         return out;
48     }
49
50     public boolean verify(InputStream in)
51     {
52         CRC32 JavaDoc crc = new CRC32 JavaDoc();
53         int b;
54     try {
55         while ((b=in.read())>=0) {
56         crc.update(b);
57         }
58     } catch (IOException e) {
59         throw new RuntimeException JavaDoc("Unexpected IOException!");
60     }
61         int val = (int)crc.getValue();
62         byte[] buf = getStringValue("Contents");
63         return val == ((buf[0]<<24 & 0xff000000) +
64                        (buf[1]<<16 & 0xff0000) +
65                        (buf[2]<<8 & 0xff00) +
66                        (buf[3] & 0xff));
67     }
68     
69     public String JavaDoc[] getLayerNames()
70     {
71         final String JavaDoc[] names = { "n0" };
72         return names;
73     }
74
75     public PDFCanvas getLayerAppearance(String JavaDoc name, PDFStyle style)
76     {
77     if (name.equals("n0")) {
78         PDFCanvas canvas = new PDFCanvas(100, 100);
79         canvas.setStyle(style);
80         LayoutBox box = new LayoutBox(80);
81         box.addTextNoBreak("CRC-32", style, null);
82         canvas.drawLayoutBox(box, 10, 50);
83         return canvas;
84     } else {
85         // This will never happen, as we're specifying the only layer is "n0"
86
// in the method above. Still, it's an example so we'll do it properly.
87
throw new IllegalArgumentException JavaDoc("Unknown layer '"+name+"' requested");
88     }
89     }
90 }
91
Popular Tags