licensing update to APSL 2.0
[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 package org.ibex.crypto;
6
7 public class HMAC extends Digest {
8     private final Digest h;
9     private final byte[] digest;
10     private final byte[] k_ipad = new byte[64];
11     private final byte[] k_opad = new byte[64];
12     
13     public int getDigestSize() { return h.getDigestSize(); }
14     public HMAC(Digest h, byte[] key) {
15         this.h = h;
16         if(key.length > 64) {
17             h.reset();
18             h.update(key,0,key.length);
19             key = new byte[h.getDigestSize()];
20             h.doFinal(key,0);
21         }
22         digest = new byte[h.getDigestSize()];
23         for(int i=0;i<64;i++) {
24             byte b = i < key.length ? key[i] : 0;
25             k_ipad[i] = (byte)(b ^ 0x36);
26             k_opad[i] = (byte)(b ^ 0x5C);
27         }
28         reset();
29     }
30     public void reset() {
31         h.reset();
32         h.update(k_ipad,0,64);
33     }
34     public void update(byte[] b, int off, int len) { h.update(b,off,len); }
35     public void doFinal(byte[] out, int off){
36         h.doFinal(digest,0);
37         h.update(k_opad,0,64);
38         h.update(digest,0,digest.length);
39         h.doFinal(out,off);
40         reset();
41     }
42     protected void processWord(byte[] in, int inOff) {}
43     protected void processLength(long bitLength) {}
44     protected void processBlock() {}
45 }