| OLD | NEW |
| (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 // Constants. | |
| 8 const _MASK_8 = 0xff; | |
| 9 const _MASK_32 = 0xffffffff; | |
| 10 const _BITS_PER_BYTE = 8; | |
| 11 const _BYTES_PER_WORD = 4; | |
| 12 | |
| 13 // Helper functions used by more than one hasher. | |
| 14 | |
| 15 // Rotate left limiting to unsigned 32-bit values. | |
| 16 int _rotl32(int val, int shift) { | |
| 17 var mod_shift = shift & 31; | |
| 18 return ((val << mod_shift) & _MASK_32) | | |
| 19 ((val & _MASK_32) >> (32 - mod_shift)); | |
| 20 } | |
| 21 | |
| 22 // Base class encapsulating common behavior for cryptographic hash | |
| 23 // functions. | |
| 24 abstract class _HashBase implements Hash { | |
| 25 _HashBase(int chunkSizeInWords, | |
| 26 int digestSizeInWords, | |
| 27 bool this._bigEndianWords) | |
| 28 : _pendingData = [], | |
| 29 _currentChunk = new List(chunkSizeInWords), | |
| 30 _h = new List(digestSizeInWords), | |
| 31 _chunkSizeInWords = chunkSizeInWords, | |
| 32 _digestSizeInWords = digestSizeInWords; | |
| 33 | |
| 34 // Update the hasher with more data. | |
| 35 void add(List<int> data) { | |
| 36 if (_digestCalled) { | |
| 37 throw new StateError( | |
| 38 'Hash update method called after digest was retrieved'); | |
| 39 } | |
| 40 _lengthInBytes += data.length; | |
| 41 _pendingData.addAll(data); | |
| 42 _iterate(); | |
| 43 } | |
| 44 | |
| 45 // Finish the hash computation and return the digest string. | |
| 46 List<int> close() { | |
| 47 if (_digestCalled) { | |
| 48 return _resultAsBytes(); | |
| 49 } | |
| 50 _digestCalled = true; | |
| 51 _finalizeData(); | |
| 52 _iterate(); | |
| 53 assert(_pendingData.length == 0); | |
| 54 return _resultAsBytes(); | |
| 55 } | |
| 56 | |
| 57 // Returns the block size of the hash in bytes. | |
| 58 int get blockSize { | |
| 59 return _chunkSizeInWords * _BYTES_PER_WORD; | |
| 60 } | |
| 61 | |
| 62 // One round of the hash computation. | |
| 63 void _updateHash(List<int> m); | |
| 64 | |
| 65 // Helper methods. | |
| 66 int _add32(x, y) => (x + y) & _MASK_32; | |
| 67 int _roundUp(val, n) => (val + n - 1) & -n; | |
| 68 | |
| 69 // Compute the final result as a list of bytes from the hash words. | |
| 70 List<int> _resultAsBytes() { | |
| 71 var result = []; | |
| 72 for (var i = 0; i < _h.length; i++) { | |
| 73 result.addAll(_wordToBytes(_h[i])); | |
| 74 } | |
| 75 return result; | |
| 76 } | |
| 77 | |
| 78 // Converts a list of bytes to a chunk of 32-bit words. | |
| 79 void _bytesToChunk(List<int> data, int dataIndex) { | |
| 80 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); | |
| 81 | |
| 82 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { | |
| 83 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3]; | |
| 84 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2]; | |
| 85 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1]; | |
| 86 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex]; | |
| 87 dataIndex += 4; | |
| 88 var word = (w3 & 0xff) << 24; | |
| 89 word |= (w2 & _MASK_8) << 16; | |
| 90 word |= (w1 & _MASK_8) << 8; | |
| 91 word |= (w0 & _MASK_8); | |
| 92 _currentChunk[wordIndex] = word; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 // Convert a 32-bit word to four bytes. | |
| 97 List<int> _wordToBytes(int word) { | |
| 98 List<int> bytes = new List(_BYTES_PER_WORD); | |
| 99 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; | |
| 100 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; | |
| 101 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; | |
| 102 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; | |
| 103 return bytes; | |
| 104 } | |
| 105 | |
| 106 // Iterate through data updating the hash computation for each | |
| 107 // chunk. | |
| 108 void _iterate() { | |
| 109 var len = _pendingData.length; | |
| 110 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | |
| 111 if (len >= chunkSizeInBytes) { | |
| 112 var index = 0; | |
| 113 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) { | |
| 114 _bytesToChunk(_pendingData, index); | |
| 115 _updateHash(_currentChunk); | |
| 116 } | |
| 117 _pendingData = _pendingData.sublist(index, len); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 // Finalize the data. Add a 1 bit to the end of the message. Expand with | |
| 122 // 0 bits and add the length of the message. | |
| 123 void _finalizeData() { | |
| 124 _pendingData.add(0x80); | |
| 125 var contentsLength = _lengthInBytes + 9; | |
| 126 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | |
| 127 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes); | |
| 128 var zeroPadding = finalizedLength - contentsLength; | |
| 129 for (var i = 0; i < zeroPadding; i++) { | |
| 130 _pendingData.add(0); | |
| 131 } | |
| 132 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; | |
| 133 assert(lengthInBits < pow(2, 32)); | |
| 134 if (_bigEndianWords) { | |
| 135 _pendingData.addAll(_wordToBytes(0)); | |
| 136 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | |
| 137 } else { | |
| 138 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | |
| 139 _pendingData.addAll(_wordToBytes(0)); | |
| 140 } | |
| 141 } | |
| 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 } | |
| OLD | NEW |