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

Side by Side Diff: mojo/public/dart/third_party/crypto/lib/src/hmac.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 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
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 part of crypto;
6
7 /**
8 * Hash-based Message Authentication Code support.
9 *
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.
12 */
13 // TODO(floitsch): make Hash implement Sink, EventSink or similar.
14 class HMAC {
15 final List<int> _message;
16 Hash _hash;
17 List<int> _key;
18 bool _isClosed = false;
19
20 /**
21 * Create an [HMAC] object from a [Hash] and a key.
22 */
23 HMAC(Hash this._hash, List<int> this._key): _message = [];
24
25 /**
26 * Add a list of bytes to the message.
27 */
28 void add(List<int> data) {
29 if (_isClosed) throw new StateError("HMAC is closed");
30 _message.addAll(data);
31 }
32
33 /**
34 * Extract the message digest as a list of bytes without closing [this].
35 */
36 List<int> get digest {
37 var blockSize = _hash.blockSize;
38
39 // Hash the key if it is longer than the block size of the hash.
40 if (_key.length > blockSize) {
41 _hash = _hash.newInstance();
42 _hash.add(_key);
43 _key = _hash.close();
44 }
45
46 // Zero-pad the key until its size is equal to the block size of the hash.
47 if (_key.length < blockSize) {
48 var newKey = new List(blockSize);
49 newKey.setRange(0, _key.length, _key);
50 for (var i = _key.length; i < blockSize; i++) {
51 newKey[i] = 0;
52 }
53 _key = newKey;
54 }
55
56 // Compute inner padding.
57 var padding = new List(blockSize);
58 for (var i = 0; i < blockSize; i++) {
59 padding[i] = 0x36 ^ _key[i];
60 }
61
62 // Inner hash computation.
63 _hash = _hash.newInstance();
64 _hash.add(padding);
65 _hash.add(_message);
66 var innerHash = _hash.close();
67
68 // Compute outer padding.
69 for (var i = 0; i < blockSize; i++) {
70 padding[i] = 0x5c ^ _key[i];
71 }
72
73 // Outer hash computation which is the result.
74 _hash = _hash.newInstance();
75 _hash.add(padding);
76 _hash.add(innerHash);
77 return _hash.close();
78 }
79
80 /**
81 * Perform the actual computation and extract the message digest
82 * as a list of bytes.
83 */
84 List<int> close() {
85 _isClosed = true;
86 return digest;
87 }
88
89 /**
90 * Verify that the HMAC computed for the data so far matches the
91 * given message digest.
92 *
93 * This method should be used instead of memcmp-style comparisons
94 * to avoid leaking information via timing.
95 *
96 * Throws an exception if the given digest does not have the same
97 * size as the digest computed by this HMAC instance.
98 */
99 bool verify(List<int> digest) {
100 var computedDigest = this.digest;
101 if (digest.length != computedDigest.length) {
102 throw new ArgumentError(
103 'Invalid digest size: ${digest.length} in HMAC.verify. '
104 'Expected: ${_hash.blockSize}.');
105 }
106 int result = 0;
107 for (var i = 0; i < digest.length; i++) {
108 result |= digest[i] ^ computedDigest[i];
109 }
110 return result == 0;
111 }
112 }
OLDNEW
« no previous file with comments | « mojo/public/dart/third_party/crypto/lib/src/hash_utils.dart ('k') | mojo/public/dart/third_party/crypto/lib/src/md5.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698