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

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

Issue 10170027: Implement HMAC support in lib/crypto. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update after test renaming. Added back lib testing in test.dart." 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
« no previous file with comments | « lib/crypto/sha256.dart ('k') | tests/lib/crypto/hmac_sha1_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Constants. 5 // Constants.
6 final _MASK_8 = 0xff; 6 final _MASK_8 = 0xff;
7 final _MASK_32 = 0xffffffff; 7 final _MASK_32 = 0xffffffff;
8 final _BITS_PER_BYTE = 8; 8 final _BITS_PER_BYTE = 8;
9 final _BYTES_PER_WORD = 4; 9 final _BYTES_PER_WORD = 4;
10 10
11 // Base class encapsulating common behavior for SHA cryptographic hash 11 // Base class encapsulating common behavior for SHA cryptographic hash
12 // functions. 12 // functions.
13 class _SHACryptoHashBase implements CryptoHash { 13 class _SHAHashBase implements Hash {
14 _SHACryptoHashBase(int this._chunkSizeInWords, int this._digestSizeInWords) 14 _SHAHashBase(int this._chunkSizeInWords, int this._digestSizeInWords)
15 : _pendingData = [] { 15 : _pendingData = [] {
16 _currentChunk = new List(_chunkSizeInWords); 16 _currentChunk = new List(_chunkSizeInWords);
17 _h = new List(_digestSizeInWords); 17 _h = new List(_digestSizeInWords);
18 } 18 }
19 19
20 // Update the hasher with more data. 20 // Update the hasher with more data.
21 _SHACryptoHashBase update(List<int> data) { 21 _SHAHashBase update(List<int> data) {
22 if (_digestCalled) { 22 if (_digestCalled) {
23 throw new CryptoHashException( 23 throw new HashException(
24 'CryptoHash update method called after digest'); 24 'Hash update method called after digest was retrieved');
25 } 25 }
26 _lengthInBytes += data.length; 26 _lengthInBytes += data.length;
27 _pendingData.addAll(data); 27 _pendingData.addAll(data);
28 _iterate(); 28 _iterate();
29 return this; 29 return this;
30 } 30 }
31 31
32 // Finish the hash computation and return the digest string. 32 // Finish the hash computation and return the digest string.
33 List<int> digest() { 33 List<int> digest() {
34 if (_digestCalled) { 34 if (_digestCalled) {
35 throw new CryptoHashException( 35 return _resultAsBytes();
36 'CryptoHash digest method called more than once');
37 } 36 }
38 _digestCalled = true; 37 _digestCalled = true;
39 _finalizeData(); 38 _finalizeData();
40 _iterate(); 39 _iterate();
41 assert(_pendingData.length == 0); 40 assert(_pendingData.length == 0);
42 var result = []; 41 return _resultAsBytes();
43 for (var i = 0; i < _h.length; i++) {
44 result.addAll(_wordToBytes(_h[i]));
45 }
46 return result;
47 } 42 }
48 43
44 // Returns the block size of the hash in bytes.
45 int get blockSize() {
46 return _chunkSizeInWords * _BYTES_PER_WORD;
47 }
48
49 // Create a fresh instance of this Hash.
50 abstract newInstance();
51
49 // One round of the hash computation. 52 // One round of the hash computation.
50 abstract _updateHash(List<int> m); 53 abstract _updateHash(List<int> m);
51 54
52 // Helper methods. 55 // Helper methods.
53 _add32(x, y) => (x + y) & _MASK_32; 56 _add32(x, y) => (x + y) & _MASK_32;
54 _roundUp(val, n) => (val + n - 1) & -n; 57 _roundUp(val, n) => (val + n - 1) & -n;
55 58
59 // Compute the final result as a list of bytes from the hash words.
60 _resultAsBytes() {
61 var result = [];
62 for (var i = 0; i < _h.length; i++) {
63 result.addAll(_wordToBytes(_h[i]));
64 }
65 return result;
66 }
67
56 // Converts a list of bytes to a chunk of 32-bit words. 68 // Converts a list of bytes to a chunk of 32-bit words.
57 _bytesToChunk(List<int> data, int dataIndex) { 69 _bytesToChunk(List<int> data, int dataIndex) {
58 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); 70 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
59 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { 71 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
60 var word = (data[dataIndex++] & 0xff) << 24; 72 var word = (data[dataIndex++] & 0xff) << 24;
61 word |= (data[dataIndex++] & _MASK_8) << 16; 73 word |= (data[dataIndex++] & _MASK_8) << 16;
62 word |= (data[dataIndex++] & _MASK_8) << 8; 74 word |= (data[dataIndex++] & _MASK_8) << 8;
63 word |= (data[dataIndex++] & _MASK_8); 75 word |= (data[dataIndex++] & _MASK_8);
64 _currentChunk[wordIndex] = word; 76 _currentChunk[wordIndex] = word;
65 } 77 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 var zeroPadding = finalizedLength - contentsLength; 113 var zeroPadding = finalizedLength - contentsLength;
102 for (var i = 0; i < zeroPadding; i++) { 114 for (var i = 0; i < zeroPadding; i++) {
103 _pendingData.add(0); 115 _pendingData.add(0);
104 } 116 }
105 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; 117 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
106 _pendingData.addAll(_wordToBytes(lengthInBits >> 32)); 118 _pendingData.addAll(_wordToBytes(lengthInBits >> 32));
107 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); 119 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
108 } 120 }
109 121
110 // Hasher state. 122 // Hasher state.
123 final int _chunkSizeInWords;
124 final int _digestSizeInWords;
111 int _lengthInBytes = 0; 125 int _lengthInBytes = 0;
112 int _chunkSizeInWords;
113 int _digestSizeInWords;
114 List<int> _pendingData; 126 List<int> _pendingData;
115 List<int> _currentChunk; 127 List<int> _currentChunk;
116 List<int> _h; 128 List<int> _h;
117 bool _digestCalled = false; 129 bool _digestCalled = false;
118 } 130 }
OLDNEW
« no previous file with comments | « lib/crypto/sha256.dart ('k') | tests/lib/crypto/hmac_sha1_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698