blob: 8c87a215d6d6707b32f2d8f3af09ad662bee3e13 [file] [log] [blame]
// 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.
/// Hash routines copied from private helpers in dart:io.
library hashes;
// Constants.
const _MASK_8 = 0xff;
const _MASK_32 = 0xffffffff;
const _BITS_PER_BYTE = 8;
const _BYTES_PER_WORD = 4;
const _pow2_32 = 0x100000000;
// Base class encapsulating common behavior for cryptographic hash
// functions.
abstract class _HashBase {
// 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;
_HashBase(
this._chunkSizeInWords, this._digestSizeInWords, this._bigEndianWords)
: _pendingData = [] {
_currentChunk = new List(_chunkSizeInWords);
_h = new List(_digestSizeInWords);
}
// Update the hasher with more data.
add(List<int> data) {
if (_digestCalled) {
throw new StateError(
'Hash update method called after digest was retrieved');
}
_lengthInBytes += data.length;
_pendingData.addAll(data);
_iterate();
}
// Finish the hash computation and return the digest string.
List<int> close() {
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.
newInstance();
// One round of the hash computation.
_updateHash(List<int> m);
// Helper methods.
_add32(x, y) => (x + y) & _MASK_32;
_roundUp(val, n) => (val + n - 1) & -n;
// 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));
}
// Compute the final result as a list of bytes from the hash words.
List<int> _resultAsBytes() {
var result = <int>[];
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.
List<int> _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);
}
_pendingData = _pendingData.sublist(index, len);
}
}
// 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 < _pow2_32);
if (_bigEndianWords) {
_pendingData.addAll(_wordToBytes(0));
_pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
} else {
_pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
_pendingData.addAll(_wordToBytes(0));
}
}
}
// The SHA1 hasher is used to compute an SHA1 message digest.
class SHA1 extends _HashBase {
// Construct a SHA1 hasher object.
SHA1()
: _w = new List(80),
super(16, 5, true) {
_h[0] = 0x67452301;
_h[1] = 0xEFCDAB89;
_h[2] = 0x98BADCFE;
_h[3] = 0x10325476;
_h[4] = 0xC3D2E1F0;
}
// Returns a new instance of this Hash.
SHA1 newInstance() {
return new SHA1();
}
// Compute one iteration of the SHA1 algorithm with a chunk of
// 16 32-bit pieces.
void _updateHash(List<int> m) {
assert(m.length == 16);
var a = _h[0];
var b = _h[1];
var c = _h[2];
var d = _h[3];
var e = _h[4];
for (var i = 0; i < 80; i++) {
if (i < 16) {
_w[i] = m[i];
} else {
var n = _w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16];
_w[i] = _rotl32(n, 1);
}
var t = _add32(_add32(_rotl32(a, 5), e), _w[i]);
if (i < 20) {
t = _add32(_add32(t, (b & c) | (~b & d)), 0x5A827999);
} else if (i < 40) {
t = _add32(_add32(t, (b ^ c ^ d)), 0x6ED9EBA1);
} else if (i < 60) {
t = _add32(_add32(t, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC);
} else {
t = _add32(_add32(t, b ^ c ^ d), 0xCA62C1D6);
}
e = d;
d = c;
c = _rotl32(b, 30);
b = a;
a = t & _MASK_32;
}
_h[0] = _add32(a, _h[0]);
_h[1] = _add32(b, _h[1]);
_h[2] = _add32(c, _h[2]);
_h[3] = _add32(d, _h[3]);
_h[4] = _add32(e, _h[4]);
}
List<int> _w;
}