1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import net.sourceforge.jcetaglib.exceptions.CryptoException; 25 import org.bouncycastle.jce.provider.BouncyCastleProvider; 26 import org.bouncycastle.util.encoders.Base64; 27 28 import java.io.*; 29 import java.security.PrivateKey ; 30 import java.security.PublicKey ; 31 import java.security.Security ; 32 import java.security.Signature ; 33 34 42 public class Signatures { 43 private static int BUFFERSIZE_TEXT = 64; 45 private static int BUFFERSIZE_FILE = 8192; 46 47 56 public static StringBuffer generateSIG(StringBuffer text 57 , PrivateKey signingKey 58 , String signame) 59 throws CryptoException { 60 61 ByteArrayOutputStream bao = null; 62 DataOutputStream dao = null; 63 64 try { 65 bao = new ByteArrayOutputStream(); 66 dao = new DataOutputStream(bao); 67 68 generateSIG(new ByteArrayInputStream(text.toString().getBytes()), dao, signingKey, signame, BUFFERSIZE_TEXT); 70 71 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 72 } catch (IOException ioe) { 73 ioe.printStackTrace(); 74 throw new CryptoException(ioe.getMessage()); 75 } finally { 76 if (dao != null) { 77 try { 79 dao.close(); 80 } catch (IOException e) { 81 ; 82 } 83 } 84 } 85 } 86 87 96 public static StringBuffer generateFileSIG(String file 97 , PrivateKey signingKey 98 , String signame) 99 throws CryptoException { 100 101 FileInputStream fis = null; 102 ByteArrayOutputStream bao = null; 103 DataOutputStream dao = null; 104 105 try { 106 fis = new FileInputStream(file); 107 bao = new ByteArrayOutputStream(); 108 dao = new DataOutputStream(bao); 109 110 generateSIG(fis, dao, signingKey, signame, BUFFERSIZE_FILE); 112 113 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 114 115 } catch (IOException ioe) { 116 ioe.printStackTrace(); 117 throw new CryptoException(ioe.getMessage()); 118 } finally { 119 if (dao != null) { 120 try { 122 dao.close(); 123 } catch (IOException e) { 124 ; 125 } 126 } 127 if (fis != null) { 128 try { 130 fis.close(); 131 } catch (IOException e) { 132 ; 133 } 134 } 135 } 136 } 137 138 148 public static void generateSIG(InputStream is 149 , DataOutputStream daos 150 , PrivateKey signingKey 151 , String signame 152 , int bufferlength) 153 throws CryptoException, IOException { 154 155 Signature sig = null; 156 157 try { 158 Security.addProvider(new BouncyCastleProvider()); 160 161 sig = Signature.getInstance(signame, "BC"); 162 sig.initSign(signingKey); 163 164 byte[] buffer = new byte[bufferlength]; 165 int length = 0; 166 167 while ((length = is.read(buffer)) != -1) { 169 sig.update(buffer, 0, length); 170 } 171 172 byte[] result = sig.sign(); 173 daos.write(result); 174 } catch (IOException ioe) { 175 ioe.printStackTrace(); 176 throw new IOException(ioe.getMessage()); 177 } catch (Exception ex) { 178 ex.printStackTrace(); 179 throw new CryptoException(ex.getMessage()); 180 } 181 } 182 183 193 public static boolean verifySIG(StringBuffer text 194 , StringBuffer signature 195 , PublicKey verifyKey 196 , String signame) 197 throws CryptoException { 198 199 try { 200 return verifySIG(new ByteArrayInputStream(text.toString().getBytes()), signature, verifyKey, signame, BUFFERSIZE_TEXT); 202 203 } catch (IOException ioe) { 204 ioe.printStackTrace(); 205 throw new CryptoException(ioe.getMessage()); 206 } 207 } 208 209 219 public static boolean verifyFileSIG(String file 220 , StringBuffer signature 221 , PublicKey verifyKey 222 , String signame) 223 throws CryptoException { 224 225 FileInputStream fis = null; 226 227 try { 228 fis = new FileInputStream(file); 229 230 return verifySIG(fis, signature, verifyKey, signame, BUFFERSIZE_FILE); 232 233 } catch (IOException ioe) { 234 ioe.printStackTrace(); 235 throw new CryptoException(ioe.getMessage()); 236 } finally { 237 if (fis != null) { 238 try { 240 fis.close(); 241 } catch (IOException e) { 242 ; 243 } 244 } 245 } 246 } 247 248 260 public static boolean verifySIG(InputStream is 261 , StringBuffer signature 262 , PublicKey verifyKey 263 , String signame 264 , int bufferlength) 265 throws CryptoException, IOException { 266 try { 267 Security.addProvider(new BouncyCastleProvider()); 269 270 Signature sig = Signature.getInstance(signame, "BC"); 271 byte[] sigBytes = Base64.decode(signature.toString()); 272 273 sig.initVerify(verifyKey); 274 byte[] buffer = new byte[bufferlength]; 275 int length = 0; 276 277 while ((length = is.read(buffer)) != -1) { 279 sig.update(buffer, 0, length); 280 } 281 282 if (!sig.verify(sigBytes)) { 283 return false; 284 } else { 285 return true; 286 } 287 } catch (IOException ioe) { 288 ioe.printStackTrace(); 289 throw new IOException(ioe.getMessage()); 290 } catch (Exception ex) { 291 ex.printStackTrace(); 292 throw new CryptoException(ex.getMessage()); 293 } 294 } 295 } 296 | Popular Tags |