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

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: Remove whitespace Created 7 years, 8 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
« no previous file with comments | « sdk/lib/crypto/crypto.dart ('k') | tests/lib/crypto/base64_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
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 Base 64 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.remainder(3);
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, c = 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 && ++c == 19 && j < outputLen - 2) {
81 out[j++] = CR;
82 out[j++] = LF;
83 c = 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 << 4) & 0x3F);
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];
68 result.write(table[b0 >> 2]); 97 int y = bytes[i + 1];
69 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); 98 out[j++] = lookup.codeUnitAt(x >> 2);
70 result.write(table[(b1 << 2) & 0x3f]); 99 out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F);
71 result.write('='); 100 out[j++] = lookup.codeUnitAt((y << 2) & 0x3F);
101 out[j++] = PAD;
72 } 102 }
73 103
74 return result.toString(); 104 return new String.fromCharCodes(out);
75 } 105 }
106
107 static List<int> base64StringToBytes(String input,
108 [bool ignoreInvalidCharacters = true]) {
109 int len = input.length;
110 if (len == 0) {
111 return new List<int>(0);
112 }
113
114 // Count '\r', '\n' and illegal characters, For illegal characters,
115 // if [ignoreInvalidCharacters] is false, throw an exception.
116 int extrasLen = 0;
117 for (int i = 0; i < len; i++) {
118 int c = _decodeTable[input.codeUnitAt(i)];
119 if (c < 0) {
120 extrasLen++;
121 if(c == -2 && !ignoreInvalidCharacters) {
122 throw new FormatException('Invalid character: ${input[i]}');
123 }
124 }
125 }
126
127 if ((len - extrasLen) % 4 != 0) {
128 throw new FormatException('''Size of Base 64 characters in Input
129 must be a multiple of 4. Input: $input''');
130 }
131
132 // Count pad characters, ignore illegal characters at the end.
133 int padLength = 0;
134 for (int i = len - 1; i >= 0; i--) {
135 int currentCodeUnit = input.codeUnitAt(i);
136 if (_decodeTable[currentCodeUnit] > 0) break;
137 if (currentCodeUnit == PAD) padLength++;
138 }
139 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength;
140 List<int> out = new List<int>(outputLen);
141
142 for (int i = 0, o = 0; o < outputLen;) {
143 // Accumulate 4 valid 6 bit Base 64 characters into an int.
144 int x = 0;
145 for (int j = 4; j > 0;) {
146 int c = _decodeTable[input.codeUnitAt(i++)];
147 if (c >= 0) {
148 x = ((x << 6) & 0xFFFFFF) | c;
149 j--;
150 }
151 }
152 out[o++] = x >> 16;
153 if (o < outputLen) {
154 out[o++] = (x >> 8) & 0xFF;
155 if (o < outputLen) out[o++] = x & 0xFF;
156 }
157 }
158 return out;
159 }
160
76 } 161 }
OLDNEW
« no previous file with comments | « sdk/lib/crypto/crypto.dart ('k') | tests/lib/crypto/base64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698