Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: packages/crypto/lib/src/hash_sink.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/crypto/lib/src/hash.dart ('k') | packages/crypto/lib/src/hmac.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import 'dart:typed_data';
6
7 import 'package:typed_data/typed_data.dart';
8
9 import 'digest.dart';
10 import 'utils.dart';
11
12 /// A base class for [Sink] implementations for hash algorithms.
13 ///
14 /// Subclasses should override [updateHash] and [digest].
15 abstract class HashSink implements Sink<List<int>> {
16 /// The inner sink that this should forward to.
17 final Sink<Digest> _sink;
18
19 /// Whether the hash function operates on big-endian words.
20 final Endianness _endian;
21
22 /// The words in the current chunk.
23 ///
24 /// This is an instance variable to avoid re-allocating, but its data isn't
25 /// used across invocations of [_iterate].
26 final Uint32List _currentChunk;
27
28 /// Messages with more than 2^64-1 bits are not supported.
29 /// So the maximum length in bytes is (2^64-1)/8.
30 static const _maxMessageLengthInBytes = 0x1fffffffffffffff;
31
32 /// The length of the input data so far, in bytes.
33 int _lengthInBytes = 0;
34
35 /// Data that has yet to be processed by the hash function.
36 final _pendingData = new Uint8Buffer();
37
38 /// Whether [close] has been called.
39 bool _isClosed = false;
40
41 /// The words in the current digest.
42 ///
43 /// This should be updated each time [updateHash] is called.
44 Uint32List get digest;
45
46 /// Creates a new hash.
47 ///
48 /// [chunkSizeInWords] represents the size of the input chunks processed by
49 /// the algorithm, in terms of 32-bit words.
50 HashSink(this._sink, int chunkSizeInWords,
51 {Endianness endian: Endianness.BIG_ENDIAN})
52 : _endian = endian,
53 _currentChunk = new Uint32List(chunkSizeInWords);
54
55 /// Runs a single iteration of the hash computation, updating [digest] with
56 /// the result.
57 ///
58 /// [chunk] is the current chunk, whose size is given by the
59 /// `chunkSizeInWords` parameter passed to the constructor.
60 void updateHash(Uint32List chunk);
61
62 @override
63 void add(List<int> data) {
64 if (_isClosed) throw new StateError('Hash.add() called after close().');
65 _lengthInBytes += data.length;
66 _pendingData.addAll(data);
67 _iterate();
68 }
69
70 @override
71 void close() {
72 if (_isClosed) return;
73 _isClosed = true;
74
75 _finalizeData();
76 _iterate();
77 assert(_pendingData.isEmpty);
78 _sink.add(new Digest(_byteDigest()));
79 _sink.close();
80 }
81
82 Uint8List _byteDigest() {
83 if (_endian == Endianness.HOST_ENDIAN) return digest.buffer.asUint8List();
84
85 var byteDigest = new Uint8List(digest.lengthInBytes);
86 var byteData = byteDigest.buffer.asByteData();
87 for (var i = 0; i < digest.length; i++) {
88 byteData.setUint32(i * bytesPerWord, digest[i]);
89 }
90 return byteDigest;
91 }
92
93 /// Iterates through [_pendingData], updating the hash computation for each
94 /// chunk.
95 void _iterate() {
96 var pendingDataBytes = _pendingData.buffer.asByteData();
97 var pendingDataChunks = _pendingData.length ~/ _currentChunk.lengthInBytes;
98 for (var i = 0; i < pendingDataChunks; i++) {
99 // Copy words from the pending data buffer into the current chunk buffer.
100 for (var j = 0; j < _currentChunk.length; j++) {
101 _currentChunk[j] = pendingDataBytes.getUint32(
102 i * _currentChunk.lengthInBytes + j * bytesPerWord, _endian);
103 }
104
105 // Run the hash function on the current chunk.
106 updateHash(_currentChunk);
107 }
108
109 // Remove all pending data up to the last clean chunk break.
110 _pendingData.removeRange(
111 0, pendingDataChunks * _currentChunk.lengthInBytes);
112 }
113
114 /// Finalizes [_pendingData].
115 ///
116 /// This adds a 1 bit to the end of the message, and expands it with 0 bits to
117 /// pad it out.
118 void _finalizeData() {
119 // Pad out the data with 0x80, eight 0s, and as many more 0s as we need to
120 // land cleanly on a chunk boundary.
121 _pendingData.add(0x80);
122 var contentsLength = _lengthInBytes + 9;
123 var finalizedLength = _roundUp(contentsLength, _currentChunk.lengthInBytes);
124 for (var i = 0; i < finalizedLength - contentsLength; i++) {
125 _pendingData.add(0);
126 }
127
128 if (_lengthInBytes > _maxMessageLengthInBytes) {
129 throw new UnsupportedError(
130 "Hashing is unsupported for messages with more than 2^64 bits.");
131 }
132
133 var lengthInBits = _lengthInBytes * bitsPerByte;
134
135 // Add the full length of the input data as a 64-bit value at the end of the
136 // hash.
137 var offset = _pendingData.length;
138 _pendingData.addAll(new Uint8List(8));
139 var byteData = _pendingData.buffer.asByteData();
140
141 // We're essentially doing byteData.setUint64(offset, lengthInBits, _endian)
142 // here, but that method isn't supported on dart2js so we implement it
143 // manually instead.
144 var highBits = lengthInBits >> 32;
145 var lowBits = lengthInBits & mask32;
146 if (_endian == Endianness.BIG_ENDIAN) {
147 byteData.setUint32(offset, highBits, _endian);
148 byteData.setUint32(offset + bytesPerWord, lowBits, _endian);
149 } else {
150 byteData.setUint32(offset, lowBits, _endian);
151 byteData.setUint32(offset + bytesPerWord, highBits, _endian);
152 }
153 }
154
155 /// Rounds [val] up to the next multiple of [n], as long as [n] is a power of
156 /// two.
157 int _roundUp(int val, int n) => (val + n - 1) & -n;
158 }
OLDNEW
« no previous file with comments | « packages/crypto/lib/src/hash.dart ('k') | packages/crypto/lib/src/hmac.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698