comparison src/ch/ethz/ssh2/crypto/digest/HMAC.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto.digest;
6
7 import java.security.DigestException;
8
9 /**
10 * HMAC.
11 *
12 * @author Christian Plattner
13 * @version 2.50, 03/15/10
14 */
15 public final class HMAC implements Digest
16 {
17 Digest md;
18 byte[] k_xor_ipad;
19 byte[] k_xor_opad;
20
21 byte[] tmp;
22
23 int size;
24
25 public HMAC(Digest md, byte[] key, int size) throws DigestException {
26 this.md = md;
27 this.size = size;
28
29 tmp = new byte[md.getDigestLength()];
30
31 final int BLOCKSIZE = 64;
32
33 k_xor_ipad = new byte[BLOCKSIZE];
34 k_xor_opad = new byte[BLOCKSIZE];
35
36 if (key.length > BLOCKSIZE)
37 {
38 md.reset();
39 md.update(key);
40 md.digest(tmp);
41 key = tmp;
42 }
43
44 System.arraycopy(key, 0, k_xor_ipad, 0, key.length);
45 System.arraycopy(key, 0, k_xor_opad, 0, key.length);
46
47 for (int i = 0; i < BLOCKSIZE; i++)
48 {
49 k_xor_ipad[i] ^= 0x36;
50 k_xor_opad[i] ^= 0x5C;
51 }
52 md.update(k_xor_ipad);
53 }
54
55 public final int getDigestLength()
56 {
57 return size;
58 }
59
60 public final void update(byte b)
61 {
62 md.update(b);
63 }
64
65 public final void update(byte[] b)
66 {
67 md.update(b);
68 }
69
70 public final void update(byte[] b, int off, int len)
71 {
72 md.update(b, off, len);
73 }
74
75 public final void reset()
76 {
77 md.reset();
78 md.update(k_xor_ipad);
79 }
80
81 public final void digest(byte[] out) throws DigestException {
82 digest(out, 0);
83 }
84
85 public final void digest(byte[] out, int off) throws DigestException {
86 md.digest(tmp);
87
88 md.update(k_xor_opad);
89 md.update(tmp);
90
91 md.digest(tmp);
92
93 System.arraycopy(tmp, 0, out, off, size);
94
95 md.update(k_xor_ipad);
96 }
97 }