Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/crypto/digest/HMAC.java @ 308:42b15aaa7ac7 ganymed
merge
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 30 Jul 2014 14:21:50 -0700 |
parents | 071eccdff8ea |
children |
comparison
equal
deleted
inserted
replaced
306:90e47d99ea54 | 308:42b15aaa7ac7 |
---|---|
6 | 6 |
7 import java.security.DigestException; | 7 import java.security.DigestException; |
8 | 8 |
9 /** | 9 /** |
10 * HMAC. | 10 * HMAC. |
11 * | 11 * |
12 * @author Christian Plattner | 12 * @author Christian Plattner |
13 * @version 2.50, 03/15/10 | 13 * @version 2.50, 03/15/10 |
14 */ | 14 */ |
15 public final class HMAC implements Digest | 15 public final class HMAC implements Digest { |
16 { | 16 Digest md; |
17 Digest md; | 17 byte[] k_xor_ipad; |
18 byte[] k_xor_ipad; | 18 byte[] k_xor_opad; |
19 byte[] k_xor_opad; | |
20 | 19 |
21 byte[] tmp; | 20 byte[] tmp; |
22 | 21 |
23 int size; | 22 int size; |
24 | 23 |
25 public HMAC(Digest md, byte[] key, int size) throws DigestException { | 24 public HMAC(Digest md, byte[] key, int size) throws DigestException { |
26 this.md = md; | 25 this.md = md; |
27 this.size = size; | 26 this.size = size; |
27 tmp = new byte[md.getDigestLength()]; | |
28 final int BLOCKSIZE = 64; | |
29 k_xor_ipad = new byte[BLOCKSIZE]; | |
30 k_xor_opad = new byte[BLOCKSIZE]; | |
28 | 31 |
29 tmp = new byte[md.getDigestLength()]; | 32 if (key.length > BLOCKSIZE) { |
33 md.reset(); | |
34 md.update(key); | |
35 md.digest(tmp); | |
36 key = tmp; | |
37 } | |
30 | 38 |
31 final int BLOCKSIZE = 64; | 39 System.arraycopy(key, 0, k_xor_ipad, 0, key.length); |
40 System.arraycopy(key, 0, k_xor_opad, 0, key.length); | |
32 | 41 |
33 k_xor_ipad = new byte[BLOCKSIZE]; | 42 for (int i = 0; i < BLOCKSIZE; i++) { |
34 k_xor_opad = new byte[BLOCKSIZE]; | 43 k_xor_ipad[i] ^= 0x36; |
44 k_xor_opad[i] ^= 0x5C; | |
45 } | |
35 | 46 |
36 if (key.length > BLOCKSIZE) | 47 md.update(k_xor_ipad); |
37 { | 48 } |
38 md.reset(); | |
39 md.update(key); | |
40 md.digest(tmp); | |
41 key = tmp; | |
42 } | |
43 | 49 |
44 System.arraycopy(key, 0, k_xor_ipad, 0, key.length); | 50 public final int getDigestLength() { |
45 System.arraycopy(key, 0, k_xor_opad, 0, key.length); | 51 return size; |
52 } | |
46 | 53 |
47 for (int i = 0; i < BLOCKSIZE; i++) | 54 public final void update(byte b) { |
48 { | 55 md.update(b); |
49 k_xor_ipad[i] ^= 0x36; | 56 } |
50 k_xor_opad[i] ^= 0x5C; | |
51 } | |
52 md.update(k_xor_ipad); | |
53 } | |
54 | 57 |
55 public final int getDigestLength() | 58 public final void update(byte[] b) { |
56 { | 59 md.update(b); |
57 return size; | 60 } |
58 } | |
59 | 61 |
60 public final void update(byte b) | 62 public final void update(byte[] b, int off, int len) { |
61 { | 63 md.update(b, off, len); |
62 md.update(b); | 64 } |
63 } | |
64 | 65 |
65 public final void update(byte[] b) | 66 public final void reset() { |
66 { | 67 md.reset(); |
67 md.update(b); | 68 md.update(k_xor_ipad); |
68 } | 69 } |
69 | 70 |
70 public final void update(byte[] b, int off, int len) | 71 public final void digest(byte[] out) throws DigestException { |
71 { | 72 digest(out, 0); |
72 md.update(b, off, len); | 73 } |
73 } | |
74 | 74 |
75 public final void reset() | 75 public final void digest(byte[] out, int off) throws DigestException { |
76 { | 76 md.digest(tmp); |
77 md.reset(); | 77 md.update(k_xor_opad); |
78 md.update(k_xor_ipad); | 78 md.update(tmp); |
79 } | 79 md.digest(tmp); |
80 | 80 System.arraycopy(tmp, 0, out, off, size); |
81 public final void digest(byte[] out) throws DigestException { | 81 md.update(k_xor_ipad); |
82 digest(out, 0); | 82 } |
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 } | 83 } |