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 library crypto.hash_base; | |
6 | |
7 import 'dart:typed_data'; | |
8 | |
9 import 'package:typed_data/typed_data.dart'; | |
10 | |
11 import 'hash.dart'; | |
12 import 'utils.dart'; | |
13 | |
14 /// A base class for [Hash] implementations. | |
15 /// | |
16 /// Subclasses should override [updateHash] and [digest]. | |
17 abstract class HashBase implements Hash { | |
18 /// Whether the hash function operates on big-endian words. | |
19 final Endianness _endian; | |
20 | |
21 /// The words in the current chunk. | |
22 /// | |
23 /// This is an instance variable to avoid re-allocating, but its data isn't | |
24 /// used across invocations of [_iterate]. | |
25 final Uint32List _currentChunk; | |
26 | |
27 /// The length of the input data so far, in bytes. | |
28 int _lengthInBytes = 0; | |
29 | |
30 /// Data that has yet to be processed by the hash function. | |
31 final _pendingData = new Uint8Buffer(); | |
32 | |
33 /// Whether [close] has been called. | |
34 bool _isClosed = false; | |
35 | |
36 /// The words in the current digest. | |
37 /// | |
38 /// This should be updated each time [updateHash] is called. | |
39 Uint32List get digest; | |
40 | |
41 int get blockSize => _currentChunk.lengthInBytes; | |
42 | |
43 /// Creates a new hash. | |
44 /// | |
45 /// [chunkSizeInWords] represents the size of the input chunks processed by | |
46 /// the algorithm, in terms of 32-bit words. | |
47 HashBase(int chunkSizeInWords, {Endianness endian: Endianness.BIG_ENDIAN}) | |
48 : _endian = endian, | |
49 _currentChunk = new Uint32List(chunkSizeInWords); | |
50 | |
51 /// Runs a single iteration of the hash computation, updating [digest] with | |
52 /// the result. | |
53 /// | |
54 /// [m] is the current chunk, whose size is given by the `chunkSizeInWords` | |
55 /// parameter passed to the constructor. | |
56 void updateHash(Uint32List chunk); | |
57 | |
58 void add(List<int> data) { | |
59 if (_isClosed) throw new StateError('Hash.add() called after close().'); | |
60 _lengthInBytes += data.length; | |
61 _pendingData.addAll(data); | |
62 _iterate(); | |
63 } | |
64 | |
65 List<int> close() { | |
66 if (_isClosed) return _byteDigest(); | |
67 _isClosed = true; | |
68 | |
69 _finalizeData(); | |
70 _iterate(); | |
71 assert(_pendingData.isEmpty); | |
72 return _byteDigest(); | |
73 } | |
74 | |
75 Uint8List _byteDigest() { | |
76 if (_endian == Endianness.HOST_ENDIAN) return digest.buffer.asUint8List(); | |
77 | |
78 var byteDigest = new Uint8List(digest.lengthInBytes); | |
79 var byteData = byteDigest.buffer.asByteData(); | |
80 for (var i = 0; i < digest.length; i++) { | |
81 byteData.setUint32(i * bytesPerWord, digest[i]); | |
82 } | |
83 return byteDigest; | |
84 } | |
85 | |
86 /// Iterates through [_pendingData], updating the hash computation for each | |
87 /// chunk. | |
88 void _iterate() { | |
89 var pendingDataBytes = _pendingData.buffer.asByteData(); | |
90 var pendingDataChunks = _pendingData.length ~/ _currentChunk.lengthInBytes; | |
91 for (var i = 0; i < pendingDataChunks; i++) { | |
92 // Copy words from the pending data buffer into the current chunk buffer. | |
93 for (var j = 0; j < _currentChunk.length; j++) { | |
94 _currentChunk[j] = pendingDataBytes.getUint32( | |
95 i * _currentChunk.lengthInBytes + j * bytesPerWord, _endian); | |
96 } | |
97 | |
98 // Run the hash function on the current chunk. | |
99 updateHash(_currentChunk); | |
100 } | |
101 | |
102 // Remove all pending data up to the last clean chunk break. | |
103 _pendingData.removeRange( | |
104 0, pendingDataChunks * _currentChunk.lengthInBytes); | |
105 } | |
106 | |
107 /// Finalizes [_pendingData]. | |
108 /// | |
109 /// This adds a 1 bit to the end of the message, and expands it with 0 bits to | |
110 /// pad it out. | |
111 void _finalizeData() { | |
112 // Pad out the data with 0x80, eight 0s, and as many more 0s as we need to | |
113 // land cleanly on a chunk boundary. | |
114 _pendingData.add(0x80); | |
115 var contentsLength = _lengthInBytes + 9; | |
116 var finalizedLength = _roundUp(contentsLength, _currentChunk.lengthInBytes); | |
117 for (var i = 0; i < finalizedLength - contentsLength; i++) { | |
118 _pendingData.add(0); | |
119 } | |
120 | |
121 var lengthInBits = _lengthInBytes * bitsPerByte; | |
122 if (lengthInBits > maxUint64) { | |
123 throw new UnsupportedError( | |
124 "Hashing is unsupported for messages with more than 2^64 bits."); | |
125 } | |
126 | |
127 // Add the full length of the input data as a 64-bit value at the end of the | |
128 // hash. | |
129 var offset = _pendingData.length; | |
130 _pendingData.addAll(new Uint8List(8)); | |
131 _pendingData.buffer.asByteData().setUint64(offset, lengthInBits, _endian); | |
132 } | |
133 | |
134 /// Rounds [val] up to the next multiple of [n], as long as [n] is a power of | |
135 /// two. | |
136 int _roundUp(int val, int n) => (val + n - 1) & -n; | |
137 } | |
OLD | NEW |