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.Key ; 30 import java.security.MessageDigest ; 31 import java.security.Security ; 32 33 41 public class Digesters { 42 private static int BUFFERSIZE_TEXT = 64; 44 private static int BUFFERSIZE_FILE = 8192; 45 46 54 public static StringBuffer hash(StringBuffer text 55 , String algorithm) 56 throws CryptoException { 57 58 ByteArrayOutputStream bao = null; 59 DataOutputStream dao = null; 60 61 try { 62 bao = new ByteArrayOutputStream(); 63 dao = new DataOutputStream(bao); 64 65 hash(new ByteArrayInputStream(text.toString().getBytes()), dao, algorithm, BUFFERSIZE_TEXT); 67 68 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 69 } catch (IOException ioe) { 70 ioe.printStackTrace(); 71 throw new CryptoException(ioe.getMessage()); 72 } finally { 73 if (dao != null) { 74 try { 76 dao.close(); 77 } catch (IOException e) { 78 ; 79 } 80 } 81 } 82 } 83 84 92 public static StringBuffer hashFile(String file 93 , String algorithm) 94 throws CryptoException { 95 96 FileInputStream fis = null; 97 ByteArrayOutputStream bao = null; 98 DataOutputStream dao = null; 99 100 try { 101 fis = new FileInputStream(file); 102 bao = new ByteArrayOutputStream(); 103 dao = new DataOutputStream(bao); 104 105 hash(fis, dao, algorithm, BUFFERSIZE_FILE); 107 108 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 109 110 } catch (IOException ioe) { 111 ioe.printStackTrace(); 112 throw new CryptoException(ioe.getMessage()); 113 } finally { 114 if (dao != null) { 115 try { 117 dao.close(); 118 } catch (IOException e) { 119 ; 120 } 121 } 122 if (fis != null) { 123 try { 125 fis.close(); 126 } catch (IOException e) { 127 ; 128 } 129 } 130 } 131 } 132 133 142 public static void hash(InputStream is 143 , DataOutputStream daos 144 , String algorithm 145 , int bufferlength) 146 throws CryptoException, IOException { 147 try { 148 Security.addProvider(new BouncyCastleProvider()); 150 151 MessageDigest digest = MessageDigest.getInstance(algorithm, "BC"); 152 153 byte[] buffer = new byte[bufferlength]; 154 int length = 0; 155 156 while ((length = is.read(buffer)) != -1) { 159 digest.update(buffer, 0, length); 160 } 161 byte[] result = digest.digest(); 163 daos.write(result); 164 } catch (IOException ioe) { 165 ioe.printStackTrace(); 166 throw new IOException(ioe.getMessage()); 167 } catch (Exception ex) { 168 ex.printStackTrace(); 169 throw new CryptoException(ex.getMessage()); 170 } 171 } 172 173 185 public static StringBuffer formDigest(StringBuffer text 186 , String digest 187 , String keyfile 188 , StringBuffer passphrase 189 , String algorithm) 190 throws CryptoException, IOException { 191 192 Key key = null; 193 ByteArrayOutputStream outStr = null; 194 DataOutputStream dataStr = null; 195 196 try { 197 Security.addProvider(new BouncyCastleProvider()); 198 199 key = Keystore.loadKey(algorithm, keyfile, passphrase); 201 202 outStr = new ByteArrayOutputStream(); 204 dataStr = new DataOutputStream(outStr); 205 206 dataStr.write(key.getEncoded()); 207 dataStr.writeBytes(text.toString()); 208 209 return hash(new StringBuffer (outStr.toString()), digest); 211 } catch (IOException ioe) { 212 ioe.printStackTrace(); 213 throw new IOException(ioe.getMessage()); 214 } catch (Exception ex) { 215 ex.printStackTrace(); 216 throw new CryptoException(ex.getMessage()); 217 } finally { 218 if (dataStr != null) { 219 try { 221 dataStr.close(); 222 } catch (IOException e) { 223 ; 224 } 225 } 226 } 227 } 228 } 229 | Popular Tags |