| 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 class _HMAC implements HMAC { | 5 class _HMAC implements HMAC { |
| 6 _HMAC(Hash this._hash, List<int> this._key) : _message = []; | 6 _HMAC(Hash this._hash, List<int> this._key) : _message = []; |
| 7 | 7 |
| 8 add(List<int> data) { | 8 add(List<int> data) { |
| 9 _message.addAll(data); | 9 _message.addAll(data); |
| 10 } | 10 } |
| 11 | 11 |
| 12 List<int> close() { | 12 List<int> close() { |
| 13 var blockSize = _hash.blockSize; | 13 var blockSize = _hash.blockSize; |
| 14 | 14 |
| 15 // Hash the key if it is longer than the block size of the hash. | 15 // Hash the key if it is longer than the block size of the hash. |
| 16 if (_key.length > blockSize) { | 16 if (_key.length > blockSize) { |
| 17 _hash = _hash.newInstance(); | 17 _hash = _hash.newInstance(); |
| 18 _hash.add(_key); | 18 _hash.add(_key); |
| 19 _key = _hash.close(); | 19 _key = _hash.close(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 // Zero-pad the key until its size is equal to the block size of the hash. | 22 // Zero-pad the key until its size is equal to the block size of the hash. |
| 23 if (_key.length < blockSize) { | 23 if (_key.length < blockSize) { |
| 24 var newKey = new List(blockSize); | 24 var newKey = new List.fixedLength(blockSize); |
| 25 newKey.setRange(0, _key.length, _key); | 25 newKey.setRange(0, _key.length, _key); |
| 26 for (var i = _key.length; i < blockSize; i++) { | 26 for (var i = _key.length; i < blockSize; i++) { |
| 27 newKey[i] = 0; | 27 newKey[i] = 0; |
| 28 } | 28 } |
| 29 _key = newKey; | 29 _key = newKey; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Compute inner padding. | 32 // Compute inner padding. |
| 33 var padding = new List(blockSize); | 33 var padding = new List.fixedLength(blockSize); |
| 34 for (var i = 0; i < blockSize; i++) { | 34 for (var i = 0; i < blockSize; i++) { |
| 35 padding[i] = 0x36 ^ _key[i]; | 35 padding[i] = 0x36 ^ _key[i]; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Inner hash computation. | 38 // Inner hash computation. |
| 39 _hash = _hash.newInstance(); | 39 _hash = _hash.newInstance(); |
| 40 _hash.add(padding); | 40 _hash.add(padding); |
| 41 _hash.add(_message); | 41 _hash.add(_message); |
| 42 var innerHash = _hash.close(); | 42 var innerHash = _hash.close(); |
| 43 | 43 |
| 44 // Compute outer padding. | 44 // Compute outer padding. |
| 45 for (var i = 0; i < blockSize; i++) { | 45 for (var i = 0; i < blockSize; i++) { |
| 46 padding[i] = 0x5c ^ _key[i]; | 46 padding[i] = 0x5c ^ _key[i]; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Outer hash computation which is the result. | 49 // Outer hash computation which is the result. |
| 50 _hash = _hash.newInstance(); | 50 _hash = _hash.newInstance(); |
| 51 _hash.add(padding); | 51 _hash.add(padding); |
| 52 _hash.add(innerHash); | 52 _hash.add(innerHash); |
| 53 return _hash.close(); | 53 return _hash.close(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // HMAC internal state. | 56 // HMAC internal state. |
| 57 Hash _hash; | 57 Hash _hash; |
| 58 List<int> _key; | 58 List<int> _key; |
| 59 List<int> _message; | 59 List<int> _message; |
| 60 } | 60 } |
| OLD | NEW |