Chromium Code Reviews| Index: lib/crypto/hmac.dart |
| diff --git a/lib/crypto/hmac.dart b/lib/crypto/hmac.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..923d281205b23d42a9d0c9ea5e054309aa35b561 |
| --- /dev/null |
| +++ b/lib/crypto/hmac.dart |
| @@ -0,0 +1,55 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +class _HMAC implements HMAC { |
| + _HMAC(Hash this._hash, List<int> this._key) : _message = []; |
| + |
| + HMAC update(List<int> data) { |
| + _message.addAll(data); |
| + return this; |
| + } |
| + |
| + List<int> digest() { |
| + var blockSize = _hash.blockSize; |
| + |
| + // Hash the key if it is longer than the block size of the hash. |
| + if (_key.length > blockSize) { |
| + _key = _hash.update(_key).digest(); |
| + _hash = _hash.newInstance(); |
|
Søren Gjesse
2012/04/30 09:05:23
Add and use a reset() method instead? Also as an o
Mads Ager (google)
2012/04/30 09:37:10
We explicitly do not want to do that. The reason f
|
| + } |
| + |
| + // Zero-pad the key until its size is equal to the block size of the hash. |
| + if (_key.length < blockSize) { |
| + var newKey = new List(blockSize); |
| + newKey.setRange(0, _key.length, _key); |
| + for (var i = _key.length; i < blockSize; i++) { |
| + newKey[i] = 0; |
| + } |
| + _key = newKey; |
| + } |
| + |
| + // Compute inner padding. |
| + var padding = new List(blockSize); |
| + for (var i = 0; i < blockSize; i++) { |
| + padding[i] = 0x36 ^ _key[i]; |
| + } |
| + |
| + // Inner hash computation. |
| + var innerHash = _hash.update(padding).update(_message).digest(); |
| + _hash = _hash.newInstance(); |
| + |
| + // Compute outer padding. |
| + for (var i = 0; i < blockSize; i++) { |
| + padding[i] = 0x5c ^ _key[i]; |
| + } |
| + |
| + // Outer hash computation which is the result. |
| + return _hash.update(padding).update(innerHash).digest(); |
| + } |
| + |
| + // HMAC internal state. |
| + Hash _hash; |
| + List<int> _key; |
| + List<int> _message; |
| +} |