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 part of crypto; | 5 part of crypto; |
6 | 6 |
7 // Constants. | 7 // Constants. |
8 const _MASK_8 = 0xff; | 8 const _MASK_8 = 0xff; |
9 const _MASK_32 = 0xffffffff; | 9 const _MASK_32 = 0xffffffff; |
10 const _BITS_PER_BYTE = 8; | 10 const _BITS_PER_BYTE = 8; |
11 const _BYTES_PER_WORD = 4; | 11 const _BYTES_PER_WORD = 4; |
12 | 12 |
13 // Helper functions used by more than one hasher. | 13 // Helper functions used by more than one hasher. |
14 | 14 |
15 // Rotate left limiting to unsigned 32-bit values. | 15 // Rotate left limiting to unsigned 32-bit values. |
16 int _rotl32(int val, int shift) { | 16 int _rotl32(int val, int shift) { |
17 var mod_shift = shift & 31; | 17 var mod_shift = shift & 31; |
18 return ((val << mod_shift) & _MASK_32) | | 18 return ((val << mod_shift) & _MASK_32) | |
19 ((val & _MASK_32) >> (32 - mod_shift)); | 19 ((val & _MASK_32) >> (32 - mod_shift)); |
20 } | 20 } |
21 | 21 |
22 // Base class encapsulating common behavior for cryptographic hash | 22 // Base class encapsulating common behavior for cryptographic hash |
23 // functions. | 23 // functions. |
24 abstract class _HashBase implements Hash { | 24 abstract class _HashBase implements Hash { |
| 25 final int _chunkSizeInWords; |
| 26 final int _digestSizeInWords; |
| 27 final bool _bigEndianWords; |
| 28 final List<int> _currentChunk; |
| 29 final List<int> _h; |
| 30 int _lengthInBytes = 0; |
| 31 List<int> _pendingData; |
| 32 bool _digestCalled = false; |
| 33 |
25 _HashBase(int chunkSizeInWords, | 34 _HashBase(int chunkSizeInWords, |
26 int digestSizeInWords, | 35 int digestSizeInWords, |
27 bool this._bigEndianWords) | 36 bool this._bigEndianWords) |
28 : _pendingData = [], | 37 : _pendingData = [], |
29 _currentChunk = new List(chunkSizeInWords), | 38 _currentChunk = new List(chunkSizeInWords), |
30 _h = new List(digestSizeInWords), | 39 _h = new List(digestSizeInWords), |
31 _chunkSizeInWords = chunkSizeInWords, | 40 _chunkSizeInWords = chunkSizeInWords, |
32 _digestSizeInWords = digestSizeInWords; | 41 _digestSizeInWords = digestSizeInWords; |
33 | 42 |
34 // Update the hasher with more data. | 43 // Update the hasher with more data. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; | 141 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; |
133 assert(lengthInBits < pow(2, 32)); | 142 assert(lengthInBits < pow(2, 32)); |
134 if (_bigEndianWords) { | 143 if (_bigEndianWords) { |
135 _pendingData.addAll(_wordToBytes(0)); | 144 _pendingData.addAll(_wordToBytes(0)); |
136 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 145 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
137 } else { | 146 } else { |
138 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 147 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
139 _pendingData.addAll(_wordToBytes(0)); | 148 _pendingData.addAll(_wordToBytes(0)); |
140 } | 149 } |
141 } | 150 } |
142 | |
143 // Hasher state. | |
144 final int _chunkSizeInWords; | |
145 final int _digestSizeInWords; | |
146 final bool _bigEndianWords; | |
147 int _lengthInBytes = 0; | |
148 List<int> _pendingData; | |
149 final List<int> _currentChunk; | |
150 final List<int> _h; | |
151 bool _digestCalled = false; | |
152 } | 151 } |
OLD | NEW |