Chromium Code Reviews| Index: lib/src/hash_base.dart |
| diff --git a/lib/src/hash_utils.dart b/lib/src/hash_base.dart |
| similarity index 64% |
| rename from lib/src/hash_utils.dart |
| rename to lib/src/hash_base.dart |
| index 5fd8b075fdcc02d4f95a6c553b44724f5683a30c..c325fe097b89f66ea22b0e86e64b7d584036bc3a 100644 |
| --- a/lib/src/hash_utils.dart |
| +++ b/lib/src/hash_base.dart |
| @@ -2,40 +2,31 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| -part of crypto; |
| +library crypto.hash_base; |
| -// Constants. |
| -const _MASK_8 = 0xff; |
| -const _MASK_32 = 0xffffffff; |
| -const _BITS_PER_BYTE = 8; |
| -const _BYTES_PER_WORD = 4; |
| +import 'dart:math' as math; |
| +import 'dart:typed_data'; |
| -// Helper functions used by more than one hasher. |
| - |
| -// Rotate left limiting to unsigned 32-bit values. |
| -int _rotl32(int val, int shift) { |
| - var mod_shift = shift & 31; |
| - return ((val << mod_shift) & _MASK_32) | |
| - ((val & _MASK_32) >> (32 - mod_shift)); |
| -} |
| +import 'hash.dart'; |
| +import 'utils.dart'; |
| // Base class encapsulating common behavior for cryptographic hash |
| // functions. |
| -abstract class _HashBase implements Hash { |
| +abstract class HashBase implements Hash { |
| final int _chunkSizeInWords; |
| final int _digestSizeInWords; |
| final bool _bigEndianWords; |
| final Uint32List _currentChunk; |
| - final Uint32List _h; |
| + final Uint32List h; |
|
Bob Nystrom
2015/09/16 21:18:35
Is this now publicly exposed outside of the packag
nweiz
2015/09/16 21:21:33
Only private classes extend HashBase. Their public
|
| int _lengthInBytes = 0; |
| List<int> _pendingData; |
| bool _digestCalled = false; |
| - _HashBase( |
| + HashBase( |
| int chunkSizeInWords, int digestSizeInWords, bool this._bigEndianWords) |
| : _pendingData = [], |
| _currentChunk = new Uint32List(chunkSizeInWords), |
| - _h = new Uint32List(digestSizeInWords), |
| + h = new Uint32List(digestSizeInWords), |
| _chunkSizeInWords = chunkSizeInWords, |
| _digestSizeInWords = digestSizeInWords; |
| @@ -64,28 +55,24 @@ abstract class _HashBase implements Hash { |
| // Returns the block size of the hash in bytes. |
| int get blockSize { |
| - return _chunkSizeInWords * _BYTES_PER_WORD; |
| + return _chunkSizeInWords * BYTES_PER_WORD; |
| } |
| // One round of the hash computation. |
| - void _updateHash(Uint32List m); |
| - |
| - // Helper methods. |
| - int _add32(x, y) => (x + y) & _MASK_32; |
| - int _roundUp(val, n) => (val + n - 1) & -n; |
| + void updateHash(Uint32List m); |
| // Compute the final result as a list of bytes from the hash words. |
| List<int> _resultAsBytes() { |
| var result = []; |
| - for (var i = 0; i < _h.length; i++) { |
| - result.addAll(_wordToBytes(_h[i])); |
| + for (var i = 0; i < h.length; i++) { |
| + result.addAll(_wordToBytes(h[i])); |
| } |
| return result; |
| } |
| // Converts a list of bytes to a chunk of 32-bit words. |
| void _bytesToChunk(List<int> data, int dataIndex) { |
| - assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); |
| + assert((data.length - dataIndex) >= (_chunkSizeInWords * BYTES_PER_WORD)); |
| for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { |
| var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3]; |
| @@ -94,20 +81,20 @@ abstract class _HashBase implements Hash { |
| var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex]; |
| dataIndex += 4; |
| var word = (w3 & 0xff) << 24; |
| - word |= (w2 & _MASK_8) << 16; |
| - word |= (w1 & _MASK_8) << 8; |
| - word |= (w0 & _MASK_8); |
| + word |= (w2 & MASK_8) << 16; |
| + word |= (w1 & MASK_8) << 8; |
| + word |= (w0 & MASK_8); |
| _currentChunk[wordIndex] = word; |
| } |
| } |
| // Convert a 32-bit word to four bytes. |
| List<int> _wordToBytes(int word) { |
| - List bytes = new List<int>(_BYTES_PER_WORD); |
| - bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; |
| - bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; |
| - bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; |
| - bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; |
| + List bytes = new List<int>(BYTES_PER_WORD); |
| + bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & MASK_8; |
| + bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & MASK_8; |
| + bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & MASK_8; |
| + bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & MASK_8; |
| return bytes; |
| } |
| @@ -115,12 +102,12 @@ abstract class _HashBase implements Hash { |
| // chunk. |
| void _iterate() { |
| var len = _pendingData.length; |
| - var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; |
| + var chunkSizeInBytes = _chunkSizeInWords * BYTES_PER_WORD; |
| if (len >= chunkSizeInBytes) { |
| var index = 0; |
| for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) { |
| _bytesToChunk(_pendingData, index); |
| - _updateHash(_currentChunk); |
| + updateHash(_currentChunk); |
| } |
| _pendingData = _pendingData.sublist(index, len); |
| } |
| @@ -131,19 +118,19 @@ abstract class _HashBase implements Hash { |
| void _finalizeData() { |
| _pendingData.add(0x80); |
| var contentsLength = _lengthInBytes + 9; |
| - var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; |
| - var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes); |
| + var chunkSizeInBytes = _chunkSizeInWords * BYTES_PER_WORD; |
| + var finalizedLength = roundUp(contentsLength, chunkSizeInBytes); |
| var zeroPadding = finalizedLength - contentsLength; |
| for (var i = 0; i < zeroPadding; i++) { |
| _pendingData.add(0); |
| } |
| - var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; |
| - assert(lengthInBits < pow(2, 32)); |
| + var lengthInBits = _lengthInBytes * BITS_PER_BYTE; |
| + assert(lengthInBits < math.pow(2, 32)); |
| if (_bigEndianWords) { |
| _pendingData.addAll(_wordToBytes(0)); |
| - _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
| + _pendingData.addAll(_wordToBytes(lengthInBits & MASK_32)); |
| } else { |
| - _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
| + _pendingData.addAll(_wordToBytes(lengthInBits & MASK_32)); |
| _pendingData.addAll(_wordToBytes(0)); |
| } |
| } |