| 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 dart.crypto; | 5 part of dart.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 _HashBase(int this._chunkSizeInWords, | 25 _HashBase(int this._chunkSizeInWords, |
| 26 int this._digestSizeInWords, | 26 int this._digestSizeInWords, |
| 27 bool this._bigEndianWords) | 27 bool this._bigEndianWords) |
| 28 : _pendingData = [] { | 28 : _pendingData = [] { |
| 29 _currentChunk = new List(_chunkSizeInWords); | 29 _currentChunk = new List.fixedLength(_chunkSizeInWords); |
| 30 _h = new List(_digestSizeInWords); | 30 _h = new List.fixedLength(_digestSizeInWords); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Update the hasher with more data. | 33 // Update the hasher with more data. |
| 34 _HashBase update(List<int> data) { | 34 add(List<int> data) { |
| 35 if (_digestCalled) { | 35 if (_digestCalled) { |
| 36 throw new HashException( | 36 throw new HashException( |
| 37 'Hash update method called after digest was retrieved'); | 37 'Hash update method called after digest was retrieved'); |
| 38 } | 38 } |
| 39 _lengthInBytes += data.length; | 39 _lengthInBytes += data.length; |
| 40 _pendingData.addAll(data); | 40 _pendingData.addAll(data); |
| 41 _iterate(); | 41 _iterate(); |
| 42 return this; | |
| 43 } | 42 } |
| 44 | 43 |
| 45 // Finish the hash computation and return the digest string. | 44 // Finish the hash computation and return the digest string. |
| 46 List<int> digest() { | 45 List<int> close() { |
| 47 if (_digestCalled) { | 46 if (_digestCalled) { |
| 48 return _resultAsBytes(); | 47 return _resultAsBytes(); |
| 49 } | 48 } |
| 50 _digestCalled = true; | 49 _digestCalled = true; |
| 51 _finalizeData(); | 50 _finalizeData(); |
| 52 _iterate(); | 51 _iterate(); |
| 53 assert(_pendingData.length == 0); | 52 assert(_pendingData.length == 0); |
| 54 return _resultAsBytes(); | 53 return _resultAsBytes(); |
| 55 } | 54 } |
| 56 | 55 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 var word = (w3 & 0xff) << 24; | 90 var word = (w3 & 0xff) << 24; |
| 92 word |= (w2 & _MASK_8) << 16; | 91 word |= (w2 & _MASK_8) << 16; |
| 93 word |= (w1 & _MASK_8) << 8; | 92 word |= (w1 & _MASK_8) << 8; |
| 94 word |= (w0 & _MASK_8); | 93 word |= (w0 & _MASK_8); |
| 95 _currentChunk[wordIndex] = word; | 94 _currentChunk[wordIndex] = word; |
| 96 } | 95 } |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Convert a 32-bit word to four bytes. | 98 // Convert a 32-bit word to four bytes. |
| 100 _wordToBytes(int word) { | 99 _wordToBytes(int word) { |
| 101 List<int> bytes = new List(_BYTES_PER_WORD); | 100 List<int> bytes = new List.fixedLength(_BYTES_PER_WORD); |
| 102 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; | 101 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; |
| 103 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; | 102 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; |
| 104 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; | 103 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; |
| 105 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; | 104 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; |
| 106 return bytes; | 105 return bytes; |
| 107 } | 106 } |
| 108 | 107 |
| 109 // Iterate through data updating the hash computation for each | 108 // Iterate through data updating the hash computation for each |
| 110 // chunk. | 109 // chunk. |
| 111 _iterate() { | 110 _iterate() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // Hasher state. | 146 // Hasher state. |
| 148 final int _chunkSizeInWords; | 147 final int _chunkSizeInWords; |
| 149 final int _digestSizeInWords; | 148 final int _digestSizeInWords; |
| 150 final bool _bigEndianWords; | 149 final bool _bigEndianWords; |
| 151 int _lengthInBytes = 0; | 150 int _lengthInBytes = 0; |
| 152 List<int> _pendingData; | 151 List<int> _pendingData; |
| 153 List<int> _currentChunk; | 152 List<int> _currentChunk; |
| 154 List<int> _h; | 153 List<int> _h; |
| 155 bool _digestCalled = false; | 154 bool _digestCalled = false; |
| 156 } | 155 } |
| OLD | NEW |