| 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 // Constants. | |
| 6 final _MASK_8 = 0xff; | |
| 7 final _MASK_32 = 0xffffffff; | |
| 8 final _BITS_PER_BYTE = 8; | |
| 9 final _BYTES_PER_WORD = 4; | |
| 10 | |
| 11 // Base class encapsulating common behavior for SHA cryptographic hash | |
| 12 // functions. | |
| 13 class _SHAHashBase implements Hash { | |
| 14 _SHAHashBase(int this._chunkSizeInWords, int this._digestSizeInWords) | |
| 15 : _pendingData = [] { | |
| 16 _currentChunk = new List(_chunkSizeInWords); | |
| 17 _h = new List(_digestSizeInWords); | |
| 18 } | |
| 19 | |
| 20 // Update the hasher with more data. | |
| 21 _SHAHashBase update(List<int> data) { | |
| 22 if (_digestCalled) { | |
| 23 throw new HashException( | |
| 24 'Hash update method called after digest was retrieved'); | |
| 25 } | |
| 26 _lengthInBytes += data.length; | |
| 27 _pendingData.addAll(data); | |
| 28 _iterate(); | |
| 29 return this; | |
| 30 } | |
| 31 | |
| 32 // Finish the hash computation and return the digest string. | |
| 33 List<int> digest() { | |
| 34 if (_digestCalled) { | |
| 35 return _resultAsBytes(); | |
| 36 } | |
| 37 _digestCalled = true; | |
| 38 _finalizeData(); | |
| 39 _iterate(); | |
| 40 assert(_pendingData.length == 0); | |
| 41 return _resultAsBytes(); | |
| 42 } | |
| 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 | |
| 52 // One round of the hash computation. | |
| 53 abstract _updateHash(List<int> m); | |
| 54 | |
| 55 // Helper methods. | |
| 56 _add32(x, y) => (x + y) & _MASK_32; | |
| 57 _roundUp(val, n) => (val + n - 1) & -n; | |
| 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 | |
| 68 // Converts a list of bytes to a chunk of 32-bit words. | |
| 69 _bytesToChunk(List<int> data, int dataIndex) { | |
| 70 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); | |
| 71 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { | |
| 72 var word = (data[dataIndex++] & 0xff) << 24; | |
| 73 word |= (data[dataIndex++] & _MASK_8) << 16; | |
| 74 word |= (data[dataIndex++] & _MASK_8) << 8; | |
| 75 word |= (data[dataIndex++] & _MASK_8); | |
| 76 _currentChunk[wordIndex] = word; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 // Convert a 32-bit word to four bytes. | |
| 81 _wordToBytes(int word) { | |
| 82 List<int> bytes = new List(_BYTES_PER_WORD); | |
| 83 bytes[0] = word >> 24; | |
| 84 bytes[1] = (word >> 16) & _MASK_8; | |
| 85 bytes[2] = (word >> 8) & _MASK_8; | |
| 86 bytes[3] = word & _MASK_8; | |
| 87 return bytes; | |
| 88 } | |
| 89 | |
| 90 // Iterate through data updating the hash computation for each | |
| 91 // chunk. | |
| 92 _iterate() { | |
| 93 var len = _pendingData.length; | |
| 94 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | |
| 95 if (len >= chunkSizeInBytes) { | |
| 96 var index = 0; | |
| 97 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) { | |
| 98 _bytesToChunk(_pendingData, index); | |
| 99 _updateHash(_currentChunk); | |
| 100 } | |
| 101 var remaining = len - index; | |
| 102 _pendingData = _pendingData.getRange(index, remaining); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // Finalize the data. Add a 1 bit to the end of the message. Expand with | |
| 107 // 0 bits and the length of the message. | |
| 108 _finalizeData() { | |
| 109 _pendingData.add(0x80); | |
| 110 var contentsLength = _lengthInBytes + 9; | |
| 111 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | |
| 112 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes); | |
| 113 var zeroPadding = finalizedLength - contentsLength; | |
| 114 for (var i = 0; i < zeroPadding; i++) { | |
| 115 _pendingData.add(0); | |
| 116 } | |
| 117 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; | |
| 118 _pendingData.addAll(_wordToBytes(lengthInBits >> 32)); | |
| 119 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | |
| 120 } | |
| 121 | |
| 122 // Hasher state. | |
| 123 final int _chunkSizeInWords; | |
| 124 final int _digestSizeInWords; | |
| 125 int _lengthInBytes = 0; | |
| 126 List<int> _pendingData; | |
| 127 List<int> _currentChunk; | |
| 128 List<int> _h; | |
| 129 bool _digestCalled = false; | |
| 130 } | |
| OLD | NEW |