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

Side by Side Diff: tests/standalone/io/hash_utils.dart

Issue 2968413002: Revert "Use relative URIs for core library part-of statements." (Closed)
Patch Set: Created 3 years, 5 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 | « sdk/lib/vmservice/running_isolates.dart ('k') | tests/standalone/io/web_socket_error_test.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 /// Hash routines copied from private helpers in dart:io.
6 library hashes;
7
8 // Constants.
9 const _MASK_8 = 0xff;
10 const _MASK_32 = 0xffffffff;
11 const _BITS_PER_BYTE = 8;
12 const _BYTES_PER_WORD = 4;
13 const _pow2_32 = 0x100000000;
14
15 // Base class encapsulating common behavior for cryptographic hash
16 // functions.
17 abstract class _HashBase {
18 // Hasher state.
19 final int _chunkSizeInWords;
20 final int _digestSizeInWords;
21 final bool _bigEndianWords;
22 int _lengthInBytes = 0;
23 List<int> _pendingData;
24 List<int> _currentChunk;
25 List<int> _h;
26 bool _digestCalled = false;
27
28 _HashBase(
29 this._chunkSizeInWords, this._digestSizeInWords, this._bigEndianWords)
30 : _pendingData = [] {
31 _currentChunk = new List(_chunkSizeInWords);
32 _h = new List(_digestSizeInWords);
33 }
34
35 // Update the hasher with more data.
36 add(List<int> data) {
37 if (_digestCalled) {
38 throw new StateError(
39 'Hash update method called after digest was retrieved');
40 }
41 _lengthInBytes += data.length;
42 _pendingData.addAll(data);
43 _iterate();
44 }
45
46 // Finish the hash computation and return the digest string.
47 List<int> close() {
48 if (_digestCalled) {
49 return _resultAsBytes();
50 }
51 _digestCalled = true;
52 _finalizeData();
53 _iterate();
54 assert(_pendingData.length == 0);
55 return _resultAsBytes();
56 }
57
58 // Returns the block size of the hash in bytes.
59 int get blockSize {
60 return _chunkSizeInWords * _BYTES_PER_WORD;
61 }
62
63 // Create a fresh instance of this Hash.
64 newInstance();
65
66 // One round of the hash computation.
67 _updateHash(List<int> m);
68
69 // Helper methods.
70 _add32(x, y) => (x + y) & _MASK_32;
71 _roundUp(val, n) => (val + n - 1) & -n;
72
73 // Rotate left limiting to unsigned 32-bit values.
74 int _rotl32(int val, int shift) {
75 var mod_shift = shift & 31;
76 return ((val << mod_shift) & _MASK_32) |
77 ((val & _MASK_32) >> (32 - mod_shift));
78 }
79
80 // Compute the final result as a list of bytes from the hash words.
81 List<int> _resultAsBytes() {
82 var result = <int>[];
83 for (var i = 0; i < _h.length; i++) {
84 result.addAll(_wordToBytes(_h[i]));
85 }
86 return result;
87 }
88
89 // Converts a list of bytes to a chunk of 32-bit words.
90 _bytesToChunk(List<int> data, int dataIndex) {
91 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
92
93 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
94 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3];
95 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2];
96 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1];
97 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex];
98 dataIndex += 4;
99 var word = (w3 & 0xff) << 24;
100 word |= (w2 & _MASK_8) << 16;
101 word |= (w1 & _MASK_8) << 8;
102 word |= (w0 & _MASK_8);
103 _currentChunk[wordIndex] = word;
104 }
105 }
106
107 // Convert a 32-bit word to four bytes.
108 List<int> _wordToBytes(int word) {
109 List<int> bytes = new List(_BYTES_PER_WORD);
110 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8;
111 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8;
112 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8;
113 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8;
114 return bytes;
115 }
116
117 // Iterate through data updating the hash computation for each
118 // chunk.
119 _iterate() {
120 var len = _pendingData.length;
121 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
122 if (len >= chunkSizeInBytes) {
123 var index = 0;
124 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
125 _bytesToChunk(_pendingData, index);
126 _updateHash(_currentChunk);
127 }
128 _pendingData = _pendingData.sublist(index, len);
129 }
130 }
131
132 // Finalize the data. Add a 1 bit to the end of the message. Expand with
133 // 0 bits and add the length of the message.
134 _finalizeData() {
135 _pendingData.add(0x80);
136 var contentsLength = _lengthInBytes + 9;
137 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
138 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes);
139 var zeroPadding = finalizedLength - contentsLength;
140 for (var i = 0; i < zeroPadding; i++) {
141 _pendingData.add(0);
142 }
143 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
144 assert(lengthInBits < _pow2_32);
145 if (_bigEndianWords) {
146 _pendingData.addAll(_wordToBytes(0));
147 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
148 } else {
149 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
150 _pendingData.addAll(_wordToBytes(0));
151 }
152 }
153 }
154
155 // The SHA1 hasher is used to compute an SHA1 message digest.
156 class SHA1 extends _HashBase {
157 // Construct a SHA1 hasher object.
158 SHA1()
159 : _w = new List(80),
160 super(16, 5, true) {
161 _h[0] = 0x67452301;
162 _h[1] = 0xEFCDAB89;
163 _h[2] = 0x98BADCFE;
164 _h[3] = 0x10325476;
165 _h[4] = 0xC3D2E1F0;
166 }
167
168 // Returns a new instance of this Hash.
169 SHA1 newInstance() {
170 return new SHA1();
171 }
172
173 // Compute one iteration of the SHA1 algorithm with a chunk of
174 // 16 32-bit pieces.
175 void _updateHash(List<int> m) {
176 assert(m.length == 16);
177
178 var a = _h[0];
179 var b = _h[1];
180 var c = _h[2];
181 var d = _h[3];
182 var e = _h[4];
183
184 for (var i = 0; i < 80; i++) {
185 if (i < 16) {
186 _w[i] = m[i];
187 } else {
188 var n = _w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16];
189 _w[i] = _rotl32(n, 1);
190 }
191 var t = _add32(_add32(_rotl32(a, 5), e), _w[i]);
192 if (i < 20) {
193 t = _add32(_add32(t, (b & c) | (~b & d)), 0x5A827999);
194 } else if (i < 40) {
195 t = _add32(_add32(t, (b ^ c ^ d)), 0x6ED9EBA1);
196 } else if (i < 60) {
197 t = _add32(_add32(t, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC);
198 } else {
199 t = _add32(_add32(t, b ^ c ^ d), 0xCA62C1D6);
200 }
201
202 e = d;
203 d = c;
204 c = _rotl32(b, 30);
205 b = a;
206 a = t & _MASK_32;
207 }
208
209 _h[0] = _add32(a, _h[0]);
210 _h[1] = _add32(b, _h[1]);
211 _h[2] = _add32(c, _h[2]);
212 _h[3] = _add32(d, _h[3]);
213 _h[4] = _add32(e, _h[4]);
214 }
215
216 List<int> _w;
217 }
OLDNEW
« no previous file with comments | « sdk/lib/vmservice/running_isolates.dart ('k') | tests/standalone/io/web_socket_error_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698