| 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; | 25 final int _chunkSizeInWords; |
| 26 final int _digestSizeInWords; | 26 final int _digestSizeInWords; |
| 27 final bool _bigEndianWords; | 27 final bool _bigEndianWords; |
| 28 final List<int> _currentChunk; | 28 final Uint32List _currentChunk; |
| 29 final List<int> _h; | 29 final Uint32List _h; |
| 30 int _lengthInBytes = 0; | 30 int _lengthInBytes = 0; |
| 31 List<int> _pendingData; | 31 List<int> _pendingData; |
| 32 bool _digestCalled = false; | 32 bool _digestCalled = false; |
| 33 | 33 |
| 34 _HashBase(int chunkSizeInWords, | 34 _HashBase(int chunkSizeInWords, |
| 35 int digestSizeInWords, | 35 int digestSizeInWords, |
| 36 bool this._bigEndianWords) | 36 bool this._bigEndianWords) |
| 37 : _pendingData = [], | 37 : _pendingData = [], |
| 38 _currentChunk = new List(chunkSizeInWords), | 38 _currentChunk = new Uint32List(chunkSizeInWords), |
| 39 _h = new List(digestSizeInWords), | 39 _h = new Uint32List(digestSizeInWords), |
| 40 _chunkSizeInWords = chunkSizeInWords, | 40 _chunkSizeInWords = chunkSizeInWords, |
| 41 _digestSizeInWords = digestSizeInWords; | 41 _digestSizeInWords = digestSizeInWords; |
| 42 | 42 |
| 43 // Update the hasher with more data. | 43 // Update the hasher with more data. |
| 44 void add(List<int> data) { | 44 void add(List<int> data) { |
| 45 if (_digestCalled) { | 45 if (_digestCalled) { |
| 46 throw new StateError( | 46 throw new StateError( |
| 47 'Hash update method called after digest was retrieved'); | 47 'Hash update method called after digest was retrieved'); |
| 48 } | 48 } |
| 49 _lengthInBytes += data.length; | 49 _lengthInBytes += data.length; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 62 assert(_pendingData.length == 0); | 62 assert(_pendingData.length == 0); |
| 63 return _resultAsBytes(); | 63 return _resultAsBytes(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Returns the block size of the hash in bytes. | 66 // Returns the block size of the hash in bytes. |
| 67 int get blockSize { | 67 int get blockSize { |
| 68 return _chunkSizeInWords * _BYTES_PER_WORD; | 68 return _chunkSizeInWords * _BYTES_PER_WORD; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // One round of the hash computation. | 71 // One round of the hash computation. |
| 72 void _updateHash(List<int> m); | 72 void _updateHash(Uint32List m); |
| 73 | 73 |
| 74 // Helper methods. | 74 // Helper methods. |
| 75 int _add32(x, y) => (x + y) & _MASK_32; | 75 int _add32(x, y) => (x + y) & _MASK_32; |
| 76 int _roundUp(val, n) => (val + n - 1) & -n; | 76 int _roundUp(val, n) => (val + n - 1) & -n; |
| 77 | 77 |
| 78 // Compute the final result as a list of bytes from the hash words. | 78 // Compute the final result as a list of bytes from the hash words. |
| 79 List<int> _resultAsBytes() { | 79 List<int> _resultAsBytes() { |
| 80 var result = []; | 80 var result = []; |
| 81 for (var i = 0; i < _h.length; i++) { | 81 for (var i = 0; i < _h.length; i++) { |
| 82 result.addAll(_wordToBytes(_h[i])); | 82 result.addAll(_wordToBytes(_h[i])); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 96 dataIndex += 4; | 96 dataIndex += 4; |
| 97 var word = (w3 & 0xff) << 24; | 97 var word = (w3 & 0xff) << 24; |
| 98 word |= (w2 & _MASK_8) << 16; | 98 word |= (w2 & _MASK_8) << 16; |
| 99 word |= (w1 & _MASK_8) << 8; | 99 word |= (w1 & _MASK_8) << 8; |
| 100 word |= (w0 & _MASK_8); | 100 word |= (w0 & _MASK_8); |
| 101 _currentChunk[wordIndex] = word; | 101 _currentChunk[wordIndex] = word; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Convert a 32-bit word to four bytes. | 105 // Convert a 32-bit word to four bytes. |
| 106 List<int> _wordToBytes(int word) { | 106 Uint32List _wordToBytes(int word) { |
| 107 List<int> bytes = new List(_BYTES_PER_WORD); | 107 Uint32List bytes = new Uint32List(_BYTES_PER_WORD); |
| 108 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; | 108 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; |
| 109 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; | 109 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; |
| 110 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; | 110 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; |
| 111 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; | 111 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; |
| 112 return bytes; | 112 return bytes; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Iterate through data updating the hash computation for each | 115 // Iterate through data updating the hash computation for each |
| 116 // chunk. | 116 // chunk. |
| 117 void _iterate() { | 117 void _iterate() { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 142 assert(lengthInBits < pow(2, 32)); | 142 assert(lengthInBits < pow(2, 32)); |
| 143 if (_bigEndianWords) { | 143 if (_bigEndianWords) { |
| 144 _pendingData.addAll(_wordToBytes(0)); | 144 _pendingData.addAll(_wordToBytes(0)); |
| 145 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 145 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
| 146 } else { | 146 } else { |
| 147 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 147 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
| 148 _pendingData.addAll(_wordToBytes(0)); | 148 _pendingData.addAll(_wordToBytes(0)); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 } | 151 } |
| OLD | NEW |