1 /* $Id: MACs.java,v 1.2 2000/01/20 14:59:23 gelderen Exp $ 2 * 3 * Copyright (C) 2000 The Cryptix Foundation Limited. 4 * All rights reserved. 5 * 6 * Use, modification, copying and distribution of this software is subject 7 * the terms and conditions of the Cryptix General Licence. You should have 8 * received a copy of the Cryptix General Licence along with this library; 9 * if not, you can download a copy from http://www.cryptix.org/ . 10 */ 11 package cryptix.jce.examples; 12 13 import java.io.IOException; 14 import java.io.FileInputStream; 15 import java.io.FileNotFoundException; 16 import java.io.FileOutputStream; 17 18 import java.security.InvalidKeyException; 19 import java.security.Key; 20 import java.security.NoSuchAlgorithmException; 21 import java.security.NoSuchProviderException; 22 import java.security.SecureRandom; 23 24 import javax.crypto.KeyGenerator; 25 import javax.crypto.Mac; 26 27 28 /** 29 * This class shows using macs. 30 * 31 * @author: Josef Hartmann (jhartmann@bigfoot.com) 32 * @version: $Revision: 1.2 $ 33 */ 34 public final class MACs 35 { 36 /** MAC */ 37 private javax.crypto.Mac mac = null; 38 39 /** key used for the mac. */ 40 private Key key = null; 41 42 /** 43 * Run the mac process of provider on the given file. 44 * 45 * @return boolean 46 * @param algorithm java.lang.String Signature algorithm. 47 * @param provider java.lang.String provider name. 48 * @param filename java.lang.String file to create signature. 49 */ run(String algorithm, String provider, String filename)50 public boolean run(String algorithm, String provider, String filename) 51 { 52 53 try 54 { 55 // Get the KeyGenerator 56 // FIXME: How do other providers provide a keygen for HMACs? 57 // 58 KeyGenerator kg = KeyGenerator.getInstance("HMAC",provider); 59 // Init the KeyGenerator using some random values. 60 kg.init(new SecureRandom()); 61 // Create a key. 62 key = kg.generateKey(); 63 // Get the HMAC algorithm of provider. 64 mac = Mac.getInstance(algorithm,provider); 65 // Initialize the algorithm. 66 mac.init(key); 67 } 68 catch (NoSuchAlgorithmException nsae) 69 { 70 nsae.printStackTrace(); 71 return false; 72 } 73 catch (NoSuchProviderException nspe) 74 { 75 nspe.printStackTrace(); 76 return false; 77 } 78 catch (InvalidKeyException ike) 79 { 80 ike.printStackTrace(); 81 return false; 82 } 83 84 try 85 { 86 // Read file to encrypt. 87 FileInputStream fInput = new FileInputStream(filename); 88 // Set output file. 89 FileOutputStream fOutput = 90 new FileOutputStream(filename+"."+algorithm); 91 92 byte[] buffer = new byte[8192]; 93 int length=0; 94 // Read input bytes into buffer and update the HMAC engine. 95 while ((length=fInput.read(buffer))!= -1) 96 { 97 mac.update(buffer); 98 } 99 100 // Run the algorithm and write output to file. 101 fOutput.write(mac.doFinal()); 102 103 // Close Streams. 104 fInput.close(); 105 fOutput.close(); 106 } 107 catch (FileNotFoundException fnfe) 108 { 109 fnfe.printStackTrace(); 110 return false; 111 } 112 catch (IOException ioe) 113 { 114 ioe.printStackTrace(); 115 return false; 116 } 117 118 // VERIFY: 119 // You have to do it on your own. 120 // Just use the same key and compute the hmac again. 121 // Then compare the values. 122 123 return true; 124 } 125 }