1 /*
2  * @(#) $Id: ComputeMAC.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10import javax.crypto.Mac;
11import javax.crypto.KeyGenerator;
12import javax.crypto.SecretKey;
13import java.io.FileInputStream;
14
15public class ComputeMAC {
16    public static void main(String[] unused) throws Exception{
17        String datafile = "ComputeDigest.java";
18
19        KeyGenerator kg = KeyGenerator.getInstance("DES");
20        kg.init(56); // 56 is the keysize. Fixed for DES
21        SecretKey key = kg.generateKey();
22
23        Mac mac = Mac.getInstance("HmacSHA1");
24        mac.init(key);
25
26        FileInputStream fis = new FileInputStream(datafile);
27        byte[] dataBytes = new byte[1024];
28        int nread = fis.read(dataBytes);
29        while (nread > 0) {
30            mac.update(dataBytes, 0, nread);
31            nread = fis.read(dataBytes);
32        };
33        byte[] macbytes = mac.doFinal();
34        System.out.println("MAC(in hex):: " + Util.byteArray2Hex(macbytes));
35    }
36}