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 javax.crypto.Mac; 29 import java.io.*; 30 import java.security.Key ; 31 import java.security.Security ; 32 33 41 public class Macs { 42 private static int BUFFERSIZE_TEXT = 64; 44 private static int BUFFERSIZE_FILE = 8192; 45 46 57 public static StringBuffer generateMAC(StringBuffer text 58 , String keyfile 59 , StringBuffer passphrase 60 , String algorithm 61 , String macname) 62 throws CryptoException { 63 64 ByteArrayOutputStream bao = null; 65 DataOutputStream dao = null; 66 67 try { 68 bao = new ByteArrayOutputStream(); 69 dao = new DataOutputStream(bao); 70 71 generateMAC(new ByteArrayInputStream(text.toString().getBytes()), dao, keyfile, passphrase, algorithm, macname, BUFFERSIZE_TEXT); 73 74 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 75 } catch (IOException ioe) { 76 ioe.printStackTrace(); 77 throw new CryptoException(ioe.getMessage()); 78 } finally { 79 if (dao != null) { 80 try { 82 dao.close(); 83 } catch (IOException e) { 84 ; 85 } 86 } 87 } 88 } 89 90 101 public static StringBuffer generateFileMAC(String file 102 , String keyfile 103 , StringBuffer passphrase 104 , String algorithm 105 , String macname) 106 throws CryptoException { 107 108 FileInputStream fis = null; 109 ByteArrayOutputStream bao = null; 110 DataOutputStream dao = null; 111 112 try { 113 fis = new FileInputStream(file); 114 bao = new ByteArrayOutputStream(); 115 dao = new DataOutputStream(bao); 116 117 generateMAC(fis, dao, keyfile, passphrase, algorithm, macname, BUFFERSIZE_FILE); 119 120 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 121 122 } catch (IOException ioe) { 123 ioe.printStackTrace(); 124 throw new CryptoException(ioe.getMessage()); 125 } finally { 126 if (dao != null) { 127 try { 129 dao.close(); 130 } catch (IOException e) { 131 ; 132 } 133 } 134 if (fis != null) { 135 try { 137 fis.close(); 138 } catch (IOException e) { 139 ; 140 } 141 } 142 } 143 } 144 145 157 public static void generateMAC(InputStream is 158 , DataOutputStream daos 159 , String keyfile 160 , StringBuffer passphrase 161 , String algorithm 162 , String macname 163 , int bufferlength) 164 throws CryptoException, IOException { 165 166 Key secretKey = null; 167 Mac mac = null; 168 169 try { 170 Security.addProvider(new BouncyCastleProvider()); 172 173 secretKey = Keystore.loadKey(algorithm, keyfile, passphrase); 175 176 mac = Mac.getInstance(macname, "BC"); 177 mac.init(secretKey); 178 179 byte[] buffer = new byte[bufferlength]; 180 int length = 0; 181 182 while ((length = is.read(buffer)) != -1) { 184 mac.update(buffer, 0, length); 185 } 186 187 byte[] result = mac.doFinal(); 188 daos.write(result); 189 } catch (IOException ioe) { 190 ioe.printStackTrace(); 191 throw new IOException(ioe.getMessage()); 192 } catch (Exception ex) { 193 ex.printStackTrace(); 194 throw new CryptoException(ex.getMessage()); 195 } 196 } 197 } 198 | Popular Tags |