1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.util.encoders.Base64; 24 import org.bouncycastle.crypto.macs.CBCBlockCipherMac; 25 import org.bouncycastle.crypto.engines.AESLightEngine; 26 import org.bouncycastle.crypto.BlockCipher; 27 import org.bouncycastle.crypto.params.KeyParameter; 28 import org.bouncycastle.crypto.params.ParametersWithIV; 29 30 import java.io.*; 31 import java.security.SecureRandom ; 32 33 41 public class Macs { 42 private static int BUFFERSIZE_TEXT = 64; 43 private static int BUFFERSIZE_FILE = 8192; 44 45 53 public static StringBuffer mac( 54 StringBuffer text 55 , SafeObject keybytes 56 ) throws CryptoException { 57 return mac(new ByteArrayInputStream(text.toString().getBytes()), keybytes, null, BUFFERSIZE_TEXT); 58 } 59 60 69 public static StringBuffer mac( 70 StringBuffer text 71 , SafeObject keybytes 72 , StringBuffer seed 73 ) throws CryptoException { 74 return mac(new ByteArrayInputStream(text.toString().getBytes()), keybytes, seed, BUFFERSIZE_TEXT); 75 } 76 77 87 public static StringBuffer macFromFile( 88 String file 89 , SafeObject keybytes 90 ) throws CryptoException, FileNotFoundException, IOException { 91 92 FileInputStream fis = new FileInputStream(file); 93 StringBuffer res = mac(fis, keybytes, null, BUFFERSIZE_FILE); 94 fis.close(); 95 return res; 96 } 97 98 109 public static StringBuffer macFromFile( 110 String file 111 , SafeObject keybytes 112 , StringBuffer seed 113 ) throws CryptoException, FileNotFoundException, IOException { 114 115 FileInputStream fis = new FileInputStream(file); 116 StringBuffer res = mac(fis, keybytes, seed, BUFFERSIZE_FILE); 117 fis.close(); 118 return res; 119 } 120 121 129 public static StringBuffer mac( 130 InputStream is 131 , SafeObject keybytes 132 , StringBuffer seed 133 , int buffersize 134 ) throws CryptoException { 135 136 KeyParameter key = null; 137 138 try { 139 SecureRandom sr = new SecureRandom (); 140 141 if (seed != null && !seed.equals("")) { 143 sr.setSeed(seed.toString().getBytes()); 144 } 145 146 BlockCipher cipher = new AESLightEngine(); 147 CBCBlockCipherMac mac = new CBCBlockCipherMac(cipher); 148 149 byte[] iv = new byte[cipher.getBlockSize()]; 151 sr.nextBytes(iv); 152 153 key = new KeyParameter(keybytes.getBytes()); 155 ParametersWithIV ivparam = new ParametersWithIV(key, iv); 157 158 mac.init(ivparam); 159 160 byte[] result = new byte[mac.getMacSize() + iv.length]; 161 byte[] buffer = new byte[buffersize]; 162 int length = 0; 163 164 System.arraycopy(iv, 0, result, 0, iv.length); 166 167 while ((length = is.read(buffer)) != -1) { 169 mac.update(buffer, 0, length); 170 } 171 172 mac.doFinal(result, iv.length); 173 174 return new StringBuffer (new String (Base64.encode(result))); 175 } catch (Exception ex) { 176 ex.printStackTrace(); 177 throw new CryptoException(ex.getMessage()); 178 } finally { 179 key = null; 181 if (seed != null) { 182 Clean.blank(seed); 183 seed = null; 184 } 185 } 186 } 187 188 197 public static boolean macEquals( 198 StringBuffer text 199 , StringBuffer mac 200 , SafeObject keybytes 201 ) throws CryptoException { 202 return macEquals(new ByteArrayInputStream(text.toString().getBytes()), mac, keybytes, BUFFERSIZE_TEXT); 203 } 204 205 216 public static boolean macEqualsFile( 217 String file 218 , StringBuffer mac 219 , SafeObject keybytes 220 ) throws CryptoException, FileNotFoundException, IOException { 221 222 FileInputStream fis = new FileInputStream(file); 223 boolean res = macEquals(fis, mac, keybytes, BUFFERSIZE_FILE); 224 fis.close(); 225 return res; 226 } 227 228 238 public static boolean macEquals( 239 InputStream is 240 , StringBuffer mac 241 , SafeObject keybytes 242 , int buffersize 243 ) throws CryptoException { 244 245 KeyParameter key = null; 246 247 try { 248 key = new KeyParameter(keybytes.getBytes()); 250 251 BlockCipher cipher = new AESLightEngine(); 252 CBCBlockCipherMac cbcmac = new CBCBlockCipherMac(cipher); 253 254 byte[] iv = new byte[cipher.getBlockSize()]; 256 byte[] decodedMac = new byte[cbcmac.getMacSize() + iv.length]; 257 decodedMac = Base64.decode(new String (mac)); 258 259 System.arraycopy(decodedMac, 0, iv, 0, iv.length); 260 261 ParametersWithIV ivparam = new ParametersWithIV(key, iv); 263 264 cbcmac.init(ivparam); 265 266 byte[] result = new byte[cbcmac.getMacSize() + iv.length]; 267 byte[] buffer = new byte[buffersize]; 268 int length = 0; 269 270 System.arraycopy(iv, 0, result, 0, iv.length); 272 273 while ((length = is.read(buffer)) != -1) { 275 cbcmac.update(buffer, 0, length); 276 } 277 278 cbcmac.doFinal(result, iv.length); 279 280 String rs = new String (Base64.encode(result)); 281 282 if (rs.equals(mac.toString())) { 283 return true; 284 } else { 285 return false; 286 } 287 } catch (Exception ex) { 288 ex.printStackTrace(); 289 throw new CryptoException(ex.getMessage()); 290 } finally { 291 key = null; 293 } 294 } 295 } 296 | Popular Tags |