Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: lib/crypto/hmac.dart

Issue 10170027: Implement HMAC support in lib/crypto. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address first round of review comments. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 class _HMAC implements HMAC {
6 _HMAC(Hash this._hash, List<int> this._key) : _message = [];
7
8 HMAC update(List<int> data) {
9 _message.addAll(data);
10 return this;
11 }
12
13 List<int> digest() {
14 var blockSize = _hash.blockSize;
15
16 // Hash the key if it is longer than the block size of the hash.
17 if (_key.length > blockSize) {
18 _key = _hash.update(_key).digest();
19 _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
20 }
21
22 // Zero-pad the key until its size is equal to the block size of the hash.
23 if (_key.length < blockSize) {
24 var newKey = new List(blockSize);
25 newKey.setRange(0, _key.length, _key);
26 for (var i = _key.length; i < blockSize; i++) {
27 newKey[i] = 0;
28 }
29 _key = newKey;
30 }
31
32 // Compute inner padding.
33 var padding = new List(blockSize);
34 for (var i = 0; i < blockSize; i++) {
35 padding[i] = 0x36 ^ _key[i];
36 }
37
38 // Inner hash computation.
39 var innerHash = _hash.update(padding).update(_message).digest();
40 _hash = _hash.newInstance();
41
42 // Compute outer padding.
43 for (var i = 0; i < blockSize; i++) {
44 padding[i] = 0x5c ^ _key[i];
45 }
46
47 // Outer hash computation which is the result.
48 return _hash.update(padding).update(innerHash).digest();
49 }
50
51 // HMAC internal state.
52 Hash _hash;
53 List<int> _key;
54 List<int> _message;
55 }
OLDNEW
« no previous file with comments | « lib/crypto/crypto.dart ('k') | lib/crypto/sha1.dart » ('j') | lib/crypto/sha_utils.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698