OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _CryptoUtils { | 7 class _CryptoUtils { |
8 static String bytesToHex(List<int> bytes) { | |
9 var result = new StringBuffer(); | |
10 for (var part in bytes) { | |
11 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); | |
12 } | |
13 return result.toString(); | |
14 } | |
15 | |
16 static const int PAD = 61; // '=' | 8 static const int PAD = 61; // '=' |
17 static const int CR = 13; // '\r' | 9 static const int CR = 13; // '\r' |
18 static const int LF = 10; // '\n' | 10 static const int LF = 10; // '\n' |
19 static const int LINE_LENGTH = 76; | 11 static const int LINE_LENGTH = 76; |
20 | 12 |
21 static const String _encodeTable = | 13 static const String _encodeTable = |
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 14 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
23 | 15 |
24 static const String _encodeTableUrlSafe = | 16 static const String _encodeTableUrlSafe = |
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | 17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
(...skipping 14 matching lines...) Expand all Loading... |
40 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, | 32 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, |
41 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 33 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
42 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 34 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
43 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 35 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
44 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 36 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
45 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 37 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
46 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 38 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
47 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | 39 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
48 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ]; | 40 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ]; |
49 | 41 |
| 42 static String bytesToHex(List<int> bytes) { |
| 43 var result = new StringBuffer(); |
| 44 for (var part in bytes) { |
| 45 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); |
| 46 } |
| 47 return result.toString(); |
| 48 } |
| 49 |
50 static String bytesToBase64(List<int> bytes, | 50 static String bytesToBase64(List<int> bytes, |
51 [bool urlSafe = false, | 51 [bool urlSafe = false, |
52 bool addLineSeparator = false]) { | 52 bool addLineSeparator = false]) { |
53 int len = bytes.length; | 53 int len = bytes.length; |
54 if (len == 0) { | 54 if (len == 0) { |
55 return ""; | 55 return ""; |
56 } | 56 } |
57 final String lookup = urlSafe ? _encodeTableUrlSafe : _encodeTable; | 57 final String lookup = urlSafe ? _encodeTableUrlSafe : _encodeTable; |
58 // Size of 24 bit chunks. | 58 // Size of 24 bit chunks. |
59 final int remainderLength = len.remainder(3); | 59 final int remainderLength = len.remainder(3); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 162 |
163 // Constants. | 163 // Constants. |
164 const _MASK_8 = 0xff; | 164 const _MASK_8 = 0xff; |
165 const _MASK_32 = 0xffffffff; | 165 const _MASK_32 = 0xffffffff; |
166 const _BITS_PER_BYTE = 8; | 166 const _BITS_PER_BYTE = 8; |
167 const _BYTES_PER_WORD = 4; | 167 const _BYTES_PER_WORD = 4; |
168 | 168 |
169 // Base class encapsulating common behavior for cryptographic hash | 169 // Base class encapsulating common behavior for cryptographic hash |
170 // functions. | 170 // functions. |
171 abstract class _HashBase { | 171 abstract class _HashBase { |
172 _HashBase(int this._chunkSizeInWords, | 172 // Hasher state. |
173 int this._digestSizeInWords, | 173 final int _chunkSizeInWords; |
174 bool this._bigEndianWords) | 174 final int _digestSizeInWords; |
| 175 final bool _bigEndianWords; |
| 176 int _lengthInBytes = 0; |
| 177 List<int> _pendingData; |
| 178 List<int> _currentChunk; |
| 179 List<int> _h; |
| 180 bool _digestCalled = false; |
| 181 |
| 182 _HashBase(this._chunkSizeInWords, |
| 183 this._digestSizeInWords, |
| 184 this._bigEndianWords) |
175 : _pendingData = [] { | 185 : _pendingData = [] { |
176 _currentChunk = new List(_chunkSizeInWords); | 186 _currentChunk = new List(_chunkSizeInWords); |
177 _h = new List(_digestSizeInWords); | 187 _h = new List(_digestSizeInWords); |
178 } | 188 } |
179 | 189 |
180 // Update the hasher with more data. | 190 // Update the hasher with more data. |
181 add(List<int> data) { | 191 add(List<int> data) { |
182 if (_digestCalled) { | 192 if (_digestCalled) { |
183 throw new StateError( | 193 throw new StateError( |
184 'Hash update method called after digest was retrieved'); | 194 'Hash update method called after digest was retrieved'); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; | 299 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; |
290 assert(lengthInBits < pow(2, 32)); | 300 assert(lengthInBits < pow(2, 32)); |
291 if (_bigEndianWords) { | 301 if (_bigEndianWords) { |
292 _pendingData.addAll(_wordToBytes(0)); | 302 _pendingData.addAll(_wordToBytes(0)); |
293 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 303 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
294 } else { | 304 } else { |
295 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 305 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); |
296 _pendingData.addAll(_wordToBytes(0)); | 306 _pendingData.addAll(_wordToBytes(0)); |
297 } | 307 } |
298 } | 308 } |
299 | |
300 // Hasher state. | |
301 final int _chunkSizeInWords; | |
302 final int _digestSizeInWords; | |
303 final bool _bigEndianWords; | |
304 int _lengthInBytes = 0; | |
305 List<int> _pendingData; | |
306 List<int> _currentChunk; | |
307 List<int> _h; | |
308 bool _digestCalled = false; | |
309 } | 309 } |
310 | 310 |
311 // The MD5 hasher is used to compute an MD5 message digest. | 311 // The MD5 hasher is used to compute an MD5 message digest. |
312 class _MD5 extends _HashBase { | 312 class _MD5 extends _HashBase { |
313 _MD5() : super(16, 4, false) { | 313 _MD5() : super(16, 4, false) { |
314 _h[0] = 0x67452301; | 314 _h[0] = 0x67452301; |
315 _h[1] = 0xefcdab89; | 315 _h[1] = 0xefcdab89; |
316 _h[2] = 0x98badcfe; | 316 _h[2] = 0x98badcfe; |
317 _h[3] = 0x10325476; | 317 _h[3] = 0x10325476; |
318 } | 318 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 439 |
440 _h[0] = _add32(a, _h[0]); | 440 _h[0] = _add32(a, _h[0]); |
441 _h[1] = _add32(b, _h[1]); | 441 _h[1] = _add32(b, _h[1]); |
442 _h[2] = _add32(c, _h[2]); | 442 _h[2] = _add32(c, _h[2]); |
443 _h[3] = _add32(d, _h[3]); | 443 _h[3] = _add32(d, _h[3]); |
444 _h[4] = _add32(e, _h[4]); | 444 _h[4] = _add32(e, _h[4]); |
445 } | 445 } |
446 | 446 |
447 List<int> _w; | 447 List<int> _w; |
448 } | 448 } |
OLD | NEW |