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

Side by Side Diff: sdk/lib/crypto/crypto_utils.dart

Issue 12534011: Add base64 decoder and change existing base64 encoder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: minor fix Created 7 years, 9 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 | Annotate | Revision Log
OLDNEW
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.crypto; 5 part of dart.crypto;
6 6
7 class _LineWrappingStringBuffer {
8 _LineWrappingStringBuffer(int this._lineLength) : _sb = new StringBuffer();
9
10 void write(String s) {
11 if (_lineLength != null && _currentLineLength == _lineLength) {
12 _sb.write('\r\n');
13 _currentLineLength = 0;
14 }
15 _sb.write(s);
16 _currentLineLength++;
17 }
18
19 String toString() => _sb.toString();
20
21 int _lineLength;
22 StringBuffer _sb;
23 int _currentLineLength = 0;
24 }
25
26 abstract class _CryptoUtils { 7 abstract class _CryptoUtils {
27 static String bytesToHex(List<int> bytes) { 8 static String bytesToHex(List<int> bytes) {
28 var result = new StringBuffer(); 9 var result = new StringBuffer();
29 for (var part in bytes) { 10 for (var part in bytes) {
30 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); 11 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
31 } 12 }
32 return result.toString(); 13 return result.toString();
33 } 14 }
34 15
35 static String bytesToBase64(List<int> bytes, [int lineLength]) { 16 static final int PAD = '='.codeUnitAt(0);
36 final table = 17 static final int CR = '\r'.codeUnitAt(0);
37 const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 18 static final int LF = '\n'.codeUnitAt(0);
38 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
39 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
40 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
41 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
42 '8', '9', '+', '/' ];
43 19
44 var result = new _LineWrappingStringBuffer(lineLength); 20 static final _encodeTable =
21 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".codeUni ts;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider not extracting code units here, and just
mdakin1 2013/03/21 15:27:15 Converted to String. But I am curious, I thought "
Lasse Reichstein Nielsen 2013/03/22 09:16:56 It just returns a read-only list view of the code
mdakin1 2013/03/25 17:05:02 Acknowledged.
45 22
46 // Encode all full 24-bit blocks. 23 static final _encodeTableUrlSafe =
47 var i = 0; 24 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".codeUni ts;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Ditto.
mdakin1 2013/03/21 15:27:15 Done.
48 for (; (i + 2) < bytes.length; i += 3) { 25
49 var b0 = bytes[i] & 0xff; 26 static const List<int> _decodeTable =
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Add a comment, it doesn't have to be dartdoc, to e
mdakin1 2013/03/21 15:27:15 Done.
50 var b1 = bytes[i + 1] & 0xff; 27 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2,
51 var b2 = bytes[i + 2] & 0xff; 28 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
52 result.write(table[b0 >> 2]); 29 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63,
53 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); 30 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2,
54 result.write(table[((b1 << 2) | (b2 >> 6)) & 0x3f]); 31 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider padding with spaces here so every number
mdakin1 2013/03/21 15:27:15 Done.
55 result.write(table[b2 & 0x3f]); 32 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, -2, 26,
33 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
34 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2,
35 -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,
37 -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,
39 -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,
41 -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 ];
43
44 static String bytesToBase64(List<int> bytes, [bool urlSafe, bool addLineSepara tor]) {
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Long line needs to be broken.
mdakin1 2013/03/21 15:27:15 Done.
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Default for urlSafe and addLineSeparator is null.
mdakin1 2013/03/25 17:05:02 Done.
45 int len = bytes != null ? bytes.length : 0;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider not accepting null. It may just be hiding
mdakin1 2013/03/21 15:27:15 I removed the null check, but not sure how to hand
Lasse Reichstein Nielsen 2013/03/22 09:16:56 That would make bytes.length throw, which is perfe
mdakin1 2013/03/25 17:05:02 Acknowledged. So basically, if it accepts null doc
floitsch 2013/03/25 17:45:37 correct.
46 if (len == 0) {
47 return "";
48 }
49 final List<int> lookup = urlSafe ? _encodeTable : _encodeTableUrlSafe;
50 // Size of 24 bit chunks.
51 final int cLen = len ~/ 3 * 3;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 cLen is a non-obvious abbreviated name. Consider g
mdakin1 2013/03/21 15:27:15 Done.
52 final int padLen = len - cLen;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 padLength. Is this done this way because it's fas
mdakin1 2013/03/21 15:27:15 Done. So obvious :/
53 // Size of base output.
54 int oLen = ((len ~/ 3) * 4) + (padLen > 0 ? 4 : 0);
55 // Add extra for line separators.
56 if (addLineSeparator) {
57 oLen += (oLen - 1) ~/ 76 << 1;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Parentheses around the left-hand-side of <<. Nobod
mdakin1 2013/03/21 15:27:15 Done.
58 }
59 List<int> out = new List<int>(oLen);
60
61 // Encode 24 bit chunks.
62 int j = 0, i = 0, cc = 0;
63 while (i < cLen) {
64 int x = bytes[i++] << 16 | bytes[i++] << 8 | bytes[i++];
65 out[j++] = lookup[x >> 18];
66 out[j++] = lookup[(x >> 12) & 0x3F];
67 out[j++] = lookup[(x >> 6) & 0x3F];
68 out[j++] = lookup[x & 0x3f];
69 // Add optional line separator for each 76 char output.
70 if (addLineSeparator && ++cc == 19 && j < oLen - 2) {
71 out[j++] = CR;
72 out[j++] = LF;
73 cc = 0;
74 }
56 } 75 }
57 76
58 // Deal with the last non-full block if any and add padding '='. 77 // If input length if not a multiple of 3, encode remaining add padding.
59 if (i == bytes.length - 1) { 78 if (padLen > 0) {
60 var b0 = bytes[i] & 0xff; 79 int x = bytes[cLen] << 10 | (padLen == 2 ? bytes[len - 1] << 2 : 0);
Lasse Reichstein Nielsen 2013/03/21 14:06:38 More parentheses! Does << bind harder than | ? Who
mdakin1 2013/03/21 15:27:15 Done.
61 result.write(table[b0 >> 2]); 80 out[j++] = lookup[(x >> 12) & 0x3F];
62 result.write(table[(b0 << 4) & 0x3f]); 81 out[j++] = lookup[(x >> 6) & 0x3F];
63 result.write('='); 82 out[j++] = padLen == 2 ? lookup[x & 0x3f] : PAD;
64 result.write('='); 83 out[j++] = PAD;
65 } else if (i == bytes.length - 2) {
66 var b0 = bytes[i] & 0xff;
67 var b1 = bytes[i + 1] & 0xff;
68 result.write(table[b0 >> 2]);
69 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
70 result.write(table[(b1 << 2) & 0x3f]);
71 result.write('=');
72 } 84 }
73 85
74 return result.toString(); 86 return new String.fromCharCodes(out);
75 } 87 }
88
89 static List<int> base64StringToBytes(String input, [bool ignoreErrors]) {
90 int len = input != null ? input.length : 0;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Again, feel free to throw on null.
mdakin1 2013/03/25 17:05:02 Done.
91 if (len == 0) {
92 return new List<int>(0);
93 }
94
95 // Count \r \n and illegal chars,
Lasse Reichstein Nielsen 2013/03/22 09:16:56 // Count '\r', '\n' and illegal characters. Makes
mdakin1 2013/03/25 17:05:02 Done.
96 // for illegal chars, if [ignoreErrors] is false, throw an exception.
97 int extrasLen = 0;
98 for (int i = 0; i < len; i++) {
99 int c = _decodeTable[input.codeUnitAt(i)];
100 if (c < 0) {
101 if (ignoreErrors) {
102 extrasLen++;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 So if c is -1, we increment extrasLen if we ignore
mdakin1 2013/03/25 17:05:02 Done.
103 } else if (c == -2) {
104 throw new FormatException(
105 'Invalid character ${new String.fromCharCode(input.codeUnitAt(i) )}');
Lasse Reichstein Nielsen 2013/03/22 09:16:56 ${input[i]} should be equivalent, and shorter, wh
mdakin1 2013/03/25 17:05:02 Done.
106 }
107 }
108 }
109
110 if ((len - extrasLen) % 4 != 0) {
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Is this requirement documented anywhere? Is it tes
mdakin1 2013/03/25 17:05:02 Not really documented, but in theory if you count
floitsch 2013/03/25 17:45:37 I would be fine with "ignoreInvalidCharacters". It
mdakin1 2013/03/26 09:52:17 Done.
111 throw new FormatException('Invalid input.');
floitsch 2013/03/25 17:45:37 I wonder if this shouldn't contain the input. In a
mdakin1 2013/03/26 09:52:17 Added input to error, explanation is a little bit
112 }
113
114 // Count pad characters, ignore illeal characters at the end.
115 int pad = 0;
116 for (int i = len; i > 1 && _decodeTable[input.codeUnitAt(--i)] <= 0;) {
117 if (input.codeUnitAt(i) == PAD) {
118 pad++;
119 }
120 }
121 int oLen = ((len - extrasLen) * 6 >> 3) - pad;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 More parentheses. Anything near a >> needs to be p
mdakin1 2013/03/25 17:05:02 Done.
122 List<int> out = new List<int>(oLen);
123
124 for (int i = 0, o = 0; o < oLen;) {
125 int x = 0;
126 int j = 18;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 How about: for (int j = 4; j > 0;) { int c = _de
mdakin1 2013/03/25 17:05:02 Done. Much better. Tested it, this version is inde
127 // Accumulate 4 valid 6 bit base64 characters into an int.
128 while (j >= 0) {
129 int c = _decodeTable[input.codeUnitAt(i++)];
130 if (c >= 0) {
131 x |= c << j;
132 j -= 6;
133 }
134 }
135 out[o++] = (x >> 16);
136 if (o < oLen) {
137 out[o++] = (x >> 8) & 0xFF;
138 if (o < oLen)
139 out[o++] = x & 0xFF;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Always use braces around then/else branches for a
mdakin1 2013/03/25 17:05:02 Done.
140 }
141 }
142 return out;
143 }
144
76 } 145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698