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

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: Address all comments from Florian and Lasse. 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 const int PAD = 61; // '='
36 final table = 17 static const int CR = 13; // '\r'
37 const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 18 static const int LF = 10; // '\n'
38 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 19 static const int LINE_LENGTH = 76;
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 20
44 var result = new _LineWrappingStringBuffer(lineLength); 21 static const String _encodeTable =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45 23
46 // Encode all full 24-bit blocks. 24 static const String _encodeTableUrlSafe =
47 var i = 0; 25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
48 for (; (i + 2) < bytes.length; i += 3) { 26
49 var b0 = bytes[i] & 0xff; 27 // Lookup table used for finding Base 64 alphabet index of a given byte.
50 var b1 = bytes[i + 1] & 0xff; 28 // -2 : Outside base64 alphabet.
51 var b2 = bytes[i + 2] & 0xff; 29 // -1 : \r or \n
52 result.write(table[b0 >> 2]); 30 // 0 : = (Padding character).
53 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); 31 // >0 : Base 64 alphabet index of given byte.
54 result.write(table[((b1 << 2) | (b2 >> 6)) & 0x3f]); 32 static const List<int> _decodeTable =
55 result.write(table[b2 & 0x3f]); 33 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2,
34 -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, 62, -2, 62, -2, 63,
36 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2,
37 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
38 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63,
39 -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
40 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,
42 -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,
44 -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,
46 -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,
48 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ];
49
50 static String bytesToBase64(List<int> bytes,
51 [bool urlSafe = false,
52 bool addLineSeparator = false]) {
53 int len = bytes.length;
54 if (len == 0) {
55 return "";
56 }
57 final String lookup = urlSafe ? _encodeTableUrlSafe : _encodeTable;
58 // Size of 24 bit chunks.
59 final int remainderLength = len % 3;
floitsch 2013/03/25 17:45:37 Since it's called "remainderLength" you could use
mdakin1 2013/03/26 09:52:17 Done. I agree, remainder is more readable.
60 final int chunkLength = len - remainderLength;
61 // Size of base output.
62 int outputLen = ((len ~/ 3) * 4) + ((remainderLength > 0) ? 4 : 0);
63 // Add extra for line separators.
64 if (addLineSeparator) {
65 outputLen += ((outputLen - 1) ~/ LINE_LENGTH) << 1;
66 }
67 List<int> out = new List<int>(outputLen);
68
69 // Encode 24 bit chunks.
70 int j = 0, i = 0, cc = 0;
71 while (i < chunkLength) {
72 int x = ((bytes[i++] << 16) & 0xFFFFFF) |
73 ((bytes[i++] << 8) & 0xFFFFFF) |
74 bytes[i++];
75 out[j++] = lookup.codeUnitAt(x >> 18);
76 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F);
77 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F);
78 out[j++] = lookup.codeUnitAt(x & 0x3f);
79 // Add optional line separator for each 76 char output.
80 if (addLineSeparator && ++cc == 19 && j < outputLen - 2) {
81 out[j++] = CR;
82 out[j++] = LF;
83 cc = 0;
84 }
56 } 85 }
57 86
58 // Deal with the last non-full block if any and add padding '='. 87 // If input length if not a multiple of 3, encode remaining bytes and
59 if (i == bytes.length - 1) { 88 // add padding.
60 var b0 = bytes[i] & 0xff; 89 if (remainderLength == 1) {
61 result.write(table[b0 >> 2]); 90 int x = bytes[i++];
62 result.write(table[(b0 << 4) & 0x3f]); 91 out[j++] = lookup.codeUnitAt(x >> 2);
63 result.write('='); 92 out[j++] = lookup.codeUnitAt((x & 0x3) << 4);
64 result.write('='); 93 out[j++] = PAD;
65 } else if (i == bytes.length - 2) { 94 out[j++] = PAD;
66 var b0 = bytes[i] & 0xff; 95 } else if (remainderLength == 2) {
67 var b1 = bytes[i + 1] & 0xff; 96 int x = (bytes[i++] << 10) | (bytes[i++] << 2);
68 result.write(table[b0 >> 2]); 97 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F);
69 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); 98 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3f);
70 result.write(table[(b1 << 2) & 0x3f]); 99 out[j++] = lookup.codeUnitAt(x & 0x3F);
71 result.write('='); 100 out[j++] = PAD;
72 } 101 }
73 102
74 return result.toString(); 103 return new String.fromCharCodes(out);
75 } 104 }
105
106 static List<int> base64StringToBytes(String input,
107 [bool ignoreErrors = true]) {
108 int len = input.length;
109 if (len == 0) {
110 return new List<int>(0);
111 }
112
113 // Count '\r', '\n' and illegal characters,
114 // For illegal characters, if [ignoreErrors] is false, throw an exception.
115 int extrasLen = 0;
116 for (int i = 0; i < len; i++) {
117 int c = _decodeTable[input.codeUnitAt(i)];
118 if (c < 0) {
119 extrasLen++;
120 if(c == -2 && !ignoreErrors) {
121 throw new FormatException('Invalid character: ${input[i]}');
122 }
123 }
124 }
125
126 if ((len - extrasLen) % 4 != 0) {
127 throw new FormatException('Invalid input.');
128 }
129
130 // Count pad characters, ignore illegal characters at the end.
131 int padLength = 0;
132 for (int i = len -1; i >= 0; i--) {
floitsch 2013/03/25 17:45:37 len - 1 (space after "-")
mdakin1 2013/03/26 09:52:17 Done.
133 int currentCodeUnit = input.codeUnitAt(i);
134 if (_decodeTable[currentCodeUnit] > 0) break;
135 if (currentCodeUnit == PAD) padLength++;
136 }
137 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength;
138 List<int> out = new List<int>(outputLen);
139
140 for (int i = 0, o = 0; o < outputLen;) {
141 // Accumulate 4 valid 6 bit base64 characters into an int.
142 int x = 0;
143 for (int j = 4; j > 0;) {
144 int c = _decodeTable[input.codeUnitAt(i++)];
145 if (c >= 0) {
146 x = ((x << 6) & 0xFFFFFF) | c;
147 j--;
148 }
149 }
150 out[o++] = x >> 16;
151 if (o < outputLen) {
152 out[o++] = (x >> 8) & 0xFF;
153 if (o < outputLen) out[o++] = x & 0xFF;
154 }
155 }
156 return out;
157 }
158
76 } 159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698