1 7 8 package java.security; 9 10 import java.io.*; 11 12 101 102 public final class SignedObject implements Serializable { 103 104 private static final long serialVersionUID = 720502720485447167L; 105 106 111 112 private byte[] content; 113 private byte[] signature; 114 private String thealgorithm; 115 116 129 public SignedObject(Serializable object, PrivateKey signingKey, 130 Signature signingEngine) 131 throws IOException, InvalidKeyException , SignatureException { 132 ByteArrayOutputStream b = new ByteArrayOutputStream(); 134 ObjectOutput a = new ObjectOutputStream(b); 135 136 a.writeObject(object); 138 a.flush(); 139 a.close(); 140 this.content = b.toByteArray(); 141 b.close(); 142 143 this.sign(signingKey, signingEngine); 145 } 146 147 157 public Object getObject() 158 throws IOException, ClassNotFoundException 159 { 160 ByteArrayInputStream b = new ByteArrayInputStream(this.content); 162 ObjectInput a = new ObjectInputStream(b); 163 Object obj = a.readObject(); 164 b.close(); 165 a.close(); 166 return obj; 167 } 168 169 176 public byte[] getSignature() { 177 byte[] sig = (byte[])this.signature.clone(); 178 return sig; 179 } 180 181 186 public String getAlgorithm() { 187 return this.thealgorithm; 188 } 189 190 204 public boolean verify(PublicKey verificationKey, 205 Signature verificationEngine) 206 throws InvalidKeyException , SignatureException { 207 verificationEngine.initVerify(verificationKey); 208 verificationEngine.update((byte[])this.content.clone()); 209 return verificationEngine.verify((byte[])this.signature.clone()); 210 } 211 212 222 private void sign(PrivateKey signingKey, Signature signingEngine) 223 throws InvalidKeyException , SignatureException { 224 signingEngine.initSign(signingKey); 226 signingEngine.update((byte[])this.content.clone()); 227 this.signature = (byte[])signingEngine.sign().clone(); 228 this.thealgorithm = signingEngine.getAlgorithm(); 229 } 230 231 235 private void readObject(java.io.ObjectInputStream s) 236 throws java.io.IOException , ClassNotFoundException 237 { 238 s.defaultReadObject(); 239 content = (byte[])content.clone(); 240 signature = (byte[])signature.clone(); 241 } 242 } 243 | Popular Tags |