| 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 HMAC update(List<int> data) { | 8 HMAC update(List<int> data) { |
| 9 _message.addAll(data); | 9 _message.addAll(data); |
| 10 return this; | 10 return this; |
| 11 } | 11 } |
| 12 | 12 |
| 13 List<int> digest() { | 13 List<int> digest() { |
| 14 var blockSize = _hash.blockSize; | 14 var blockSize = _hash.blockSize; |
| 15 | 15 |
| 16 // Hash the key if it is longer than the block size of the hash. | 16 // Hash the key if it is longer than the block size of the hash. |
| 17 if (_key.length > blockSize) { | 17 if (_key.length > blockSize) { |
| 18 _hash = _hash.newInstance(); |
| 18 _key = _hash.update(_key).digest(); | 19 _key = _hash.update(_key).digest(); |
| 19 _hash = _hash.newInstance(); | |
| 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(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(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 var innerHash = _hash.update(padding).update(_message).digest(); | 40 var innerHash = _hash.update(padding).update(_message).digest(); |
| 40 _hash = _hash.newInstance(); | |
| 41 | 41 |
| 42 // Compute outer padding. | 42 // Compute outer padding. |
| 43 for (var i = 0; i < blockSize; i++) { | 43 for (var i = 0; i < blockSize; i++) { |
| 44 padding[i] = 0x5c ^ _key[i]; | 44 padding[i] = 0x5c ^ _key[i]; |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Outer hash computation which is the result. | 47 // Outer hash computation which is the result. |
| 48 _hash = _hash.newInstance(); |
| 48 return _hash.update(padding).update(innerHash).digest(); | 49 return _hash.update(padding).update(innerHash).digest(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // HMAC internal state. | 52 // HMAC internal state. |
| 52 Hash _hash; | 53 Hash _hash; |
| 53 List<int> _key; | 54 List<int> _key; |
| 54 List<int> _message; | 55 List<int> _message; |
| 55 } | 56 } |
| OLD | NEW |