cc21879aef
Only change is to avoid remaining issue that (8 >> 32) is 8 with dart2js at this point. We restrict the length of the message to "2^32 - 1" bits which. R=sgjesse@google.com,floitsch@google.com BUG= TEST= Review URL: https://chromiumcodereview.appspot.com//10388068 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@7533 260f80e4-7a28-3924-810f-c04153c831b5
155 lines
4.8 KiB
Dart
155 lines
4.8 KiB
Dart
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// 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.
|
|
|
|
// Constants.
|
|
final _MASK_8 = 0xff;
|
|
final _MASK_32 = 0xffffffff;
|
|
final _BITS_PER_BYTE = 8;
|
|
final _BYTES_PER_WORD = 4;
|
|
|
|
// 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));
|
|
}
|
|
|
|
// Base class encapsulating common behavior for cryptographic hash
|
|
// functions.
|
|
class _HashBase implements Hash {
|
|
_HashBase(int this._chunkSizeInWords,
|
|
int this._digestSizeInWords,
|
|
bool this._bigEndianWords)
|
|
: _pendingData = [] {
|
|
_currentChunk = new List(_chunkSizeInWords);
|
|
_h = new List(_digestSizeInWords);
|
|
}
|
|
|
|
// Update the hasher with more data.
|
|
_HashBase update(List<int> data) {
|
|
if (_digestCalled) {
|
|
throw new HashException(
|
|
'Hash update method called after digest was retrieved');
|
|
}
|
|
_lengthInBytes += data.length;
|
|
_pendingData.addAll(data);
|
|
_iterate();
|
|
return this;
|
|
}
|
|
|
|
// Finish the hash computation and return the digest string.
|
|
List<int> digest() {
|
|
if (_digestCalled) {
|
|
return _resultAsBytes();
|
|
}
|
|
_digestCalled = true;
|
|
_finalizeData();
|
|
_iterate();
|
|
assert(_pendingData.length == 0);
|
|
return _resultAsBytes();
|
|
}
|
|
|
|
// Returns the block size of the hash in bytes.
|
|
int get blockSize() {
|
|
return _chunkSizeInWords * _BYTES_PER_WORD;
|
|
}
|
|
|
|
// Create a fresh instance of this Hash.
|
|
abstract newInstance();
|
|
|
|
// One round of the hash computation.
|
|
abstract _updateHash(List<int> m);
|
|
|
|
// Helper methods.
|
|
_add32(x, y) => (x + y) & _MASK_32;
|
|
_roundUp(val, n) => (val + n - 1) & -n;
|
|
|
|
// Compute the final result as a list of bytes from the hash words.
|
|
_resultAsBytes() {
|
|
var result = [];
|
|
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.
|
|
_bytesToChunk(List<int> data, int dataIndex) {
|
|
assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
|
|
|
|
for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
|
|
var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3];
|
|
var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2];
|
|
var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1];
|
|
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);
|
|
_currentChunk[wordIndex] = word;
|
|
}
|
|
}
|
|
|
|
// Convert a 32-bit word to four bytes.
|
|
_wordToBytes(int word) {
|
|
List<int> bytes = new List(_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;
|
|
}
|
|
|
|
// Iterate through data updating the hash computation for each
|
|
// chunk.
|
|
_iterate() {
|
|
var len = _pendingData.length;
|
|
var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
|
|
if (len >= chunkSizeInBytes) {
|
|
var index = 0;
|
|
for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
|
|
_bytesToChunk(_pendingData, index);
|
|
_updateHash(_currentChunk);
|
|
}
|
|
var remaining = len - index;
|
|
_pendingData = _pendingData.getRange(index, remaining);
|
|
}
|
|
}
|
|
|
|
// Finalize the data. Add a 1 bit to the end of the message. Expand with
|
|
// 0 bits and add the length of the message.
|
|
_finalizeData() {
|
|
_pendingData.add(0x80);
|
|
var contentsLength = _lengthInBytes + 9;
|
|
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 < Math.pow(2, 32));
|
|
if (_bigEndianWords) {
|
|
_pendingData.addAll(_wordToBytes(0));
|
|
_pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
|
|
} else {
|
|
_pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
|
|
_pendingData.addAll(_wordToBytes(0));
|
|
}
|
|
}
|
|
|
|
// Hasher state.
|
|
final int _chunkSizeInWords;
|
|
final int _digestSizeInWords;
|
|
final bool _bigEndianWords;
|
|
int _lengthInBytes = 0;
|
|
List<int> _pendingData;
|
|
List<int> _currentChunk;
|
|
List<int> _h;
|
|
bool _digestCalled = false;
|
|
}
|