OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of crypto; | 5 part of crypto; |
6 | 6 |
7 /** | 7 /** |
8 * Hash-based Message Authentication Code support. | 8 * Hash-based Message Authentication Code support. |
9 * | 9 * |
10 * The [add] method is used to add data to the message. The [digest] and | 10 * The [add] method is used to add data to the message. The [digest] and |
11 * [close] methods are used to extract the message authentication code. | 11 * [close] methods are used to extract the message authentication code. |
12 */ | 12 */ |
13 // TODO(floitsch): make Hash implement Sink, EventSink or similar. | 13 // TODO(floitsch): make Hash implement Sink, EventSink or similar. |
14 class HMAC { | 14 class HMAC { |
| 15 final List<int> _message; |
| 16 Hash _hash; |
| 17 List<int> _key; |
15 bool _isClosed = false; | 18 bool _isClosed = false; |
16 | 19 |
17 /** | 20 /** |
18 * Create an [HMAC] object from a [Hash] and a key. | 21 * Create an [HMAC] object from a [Hash] and a key. |
19 */ | 22 */ |
20 HMAC(Hash this._hash, List<int> this._key) : _message = []; | 23 HMAC(Hash this._hash, List<int> this._key): _message = []; |
21 | 24 |
22 /** | 25 /** |
23 * Add a list of bytes to the message. | 26 * Add a list of bytes to the message. |
24 */ | 27 */ |
25 void add(List<int> data) { | 28 void add(List<int> data) { |
26 if (_isClosed) throw new StateError("HMAC is closed"); | 29 if (_isClosed) throw new StateError("HMAC is closed"); |
27 _message.addAll(data); | 30 _message.addAll(data); |
28 } | 31 } |
29 | 32 |
30 /** | 33 /** |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 throw new ArgumentError( | 102 throw new ArgumentError( |
100 'Invalid digest size: ${digest.length} in HMAC.verify. ' | 103 'Invalid digest size: ${digest.length} in HMAC.verify. ' |
101 'Expected: ${_hash.blockSize}.'); | 104 'Expected: ${_hash.blockSize}.'); |
102 } | 105 } |
103 int result = 0; | 106 int result = 0; |
104 for (var i = 0; i < digest.length; i++) { | 107 for (var i = 0; i < digest.length; i++) { |
105 result |= digest[i] ^ computedDigest[i]; | 108 result |= digest[i] ^ computedDigest[i]; |
106 } | 109 } |
107 return result == 0; | 110 return result == 0; |
108 } | 111 } |
109 | |
110 // HMAC internal state. | |
111 Hash _hash; | |
112 List<int> _key; | |
113 final List<int> _message; | |
114 } | 112 } |
OLD | NEW |