merge copyright headers with brians code
[org.ibex.crypto.git] / src / org / ibex / crypto / HMAC.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4 /*
5  * org.ibex.crypto.HMAC - By Brian Alliet
6  * Copyright (C) 2004 Brian Alliet
7  */
8
9 package org.ibex.crypto;
10
11 public class HMAC extends Digest {
12     private final Digest h;
13     private final byte[] digest;
14     private final byte[] k_ipad = new byte[64];
15     private final byte[] k_opad = new byte[64];
16     
17     public int getDigestSize() { return h.getDigestSize(); }
18     public HMAC(Digest h, byte[] key) {
19         this.h = h;
20         if(key.length > 64) {
21             h.reset();
22             h.update(key,0,key.length);
23             key = new byte[h.getDigestSize()];
24             h.doFinal(key,0);
25         }
26         digest = new byte[h.getDigestSize()];
27         for(int i=0;i<64;i++) {
28             byte b = i < key.length ? key[i] : 0;
29             k_ipad[i] = (byte)(b ^ 0x36);
30             k_opad[i] = (byte)(b ^ 0x5C);
31         }
32         reset();
33     }
34     public void reset() {
35         h.reset();
36         h.update(k_ipad,0,64);
37     }
38     public void update(byte[] b, int off, int len) { h.update(b,off,len); }
39     public void doFinal(byte[] out, int off){
40         h.doFinal(digest,0);
41         h.update(k_opad,0,64);
42         h.update(digest,0,digest.length);
43         h.doFinal(out,off);
44         reset();
45     }
46     protected void processWord(byte[] in, int inOff) {}
47     protected void processLength(long bitLength) {}
48     protected void processBlock() {}
49 }