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; | 15 final List<int> _message; |
16 Hash _hash; | 16 Hash _hash; |
17 List<int> _key; | 17 List<int> _key; |
18 bool _isClosed = false; | 18 bool _isClosed = false; |
19 | 19 |
20 /** | 20 /** |
21 * Create an [HMAC] object from a [Hash] and a key. | 21 * Create an [HMAC] object from a [Hash] and a key. |
22 */ | 22 */ |
23 HMAC(Hash this._hash, List<int> this._key): _message = []; | 23 HMAC(Hash this._hash, List<int> this._key) : _message = []; |
24 | 24 |
25 /** | 25 /** |
26 * Add a list of bytes to the message. | 26 * Add a list of bytes to the message. |
27 */ | 27 */ |
28 void add(List<int> data) { | 28 void add(List<int> data) { |
29 if (_isClosed) throw new StateError("HMAC is closed"); | 29 if (_isClosed) throw new StateError("HMAC is closed"); |
30 _message.addAll(data); | 30 _message.addAll(data); |
31 } | 31 } |
32 | 32 |
33 /** | 33 /** |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 'Invalid digest size: ${digest.length} in HMAC.verify. ' | 103 'Invalid digest size: ${digest.length} in HMAC.verify. ' |
104 'Expected: ${_hash.blockSize}.'); | 104 'Expected: ${_hash.blockSize}.'); |
105 } | 105 } |
106 int result = 0; | 106 int result = 0; |
107 for (var i = 0; i < digest.length; i++) { | 107 for (var i = 0; i < digest.length; i++) { |
108 result |= digest[i] ^ computedDigest[i]; | 108 result |= digest[i] ^ computedDigest[i]; |
109 } | 109 } |
110 return result == 0; | 110 return result == 0; |
111 } | 111 } |
112 } | 112 } |
OLD | NEW |