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

Side by Side Diff: lib/src/crypto_utils.dart

Issue 1159093002: Implement a Base64 codec (Closed) Base URL: https://github.com/dart-lang/crypto.git@master
Patch Set: Remove newlines after dart-docs Created 5 years, 6 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 | « lib/src/base64.dart ('k') | test/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 crypto; 5 part of crypto;
6 6
7 abstract class _CryptoUtils { 7 abstract class _CryptoUtils {
8 static String bytesToHex(List<int> bytes) { 8 static String bytesToHex(List<int> bytes) {
9 var result = new StringBuffer(); 9 var result = new StringBuffer();
10 for (var part in bytes) { 10 for (var part in bytes) {
11 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); 11 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
12 } 12 }
13 return result.toString(); 13 return result.toString();
14 } 14 }
15 15
16 static const int PAD = 61; // '='
17 static const int CR = 13; // '\r'
18 static const int LF = 10; // '\n'
19 static const int LINE_LENGTH = 76;
20
21 static const String _encodeTable =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23
24 static const String _encodeTableUrlSafe =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
26
27 // Lookup table used for finding Base 64 alphabet index of a given byte.
28 // -2 : Outside Base 64 alphabet.
29 // -1 : '\r' or '\n'
30 // 0 : = (Padding character).
31 // >0 : Base 64 alphabet index of given byte.
32 static const List<int> _decodeTable =
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, 16 static String bytesToBase64(List<int> bytes,
51 [bool urlSafe = false, 17 [bool urlSafe = false,
52 bool addLineSeparator = false]) { 18 bool addLineSeparator = false]) {
53 int len = bytes.length; 19 return BASE64.encode(bytes,
54 if (len == 0) { 20 urlSafe: urlSafe,
55 return ""; 21 addLineSeparator: addLineSeparator);
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 }
85 }
86
87 // If input length if not a multiple of 3, encode remaining bytes and
88 // add padding.
89 if (remainderLength == 1) {
90 int x = bytes[i];
91 out[j++] = lookup.codeUnitAt(x >> 2);
92 out[j++] = lookup.codeUnitAt((x << 4) & 0x3F);
93 out[j++] = PAD;
94 out[j++] = PAD;
95 } else if (remainderLength == 2) {
96 int x = bytes[i];
97 int y = bytes[i + 1];
98 out[j++] = lookup.codeUnitAt(x >> 2);
99 out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F);
100 out[j++] = lookup.codeUnitAt((y << 2) & 0x3F);
101 out[j++] = PAD;
102 }
103
104 return new String.fromCharCodes(out);
105 } 22 }
106 23
107 static List<int> base64StringToBytes(String input) { 24 static List<int> base64StringToBytes(String input) {
108 int len = input.length; 25 return BASE64.decode(input);
109 if (len == 0) {
110 return new List<int>(0);
111 }
112
113 // Count '\r', '\n' and illegal characters, For illegal characters,
114 // 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) {
121 throw new FormatException('Invalid character: ${input[i]}');
122 }
123 }
124 }
125
126 if ((len - extrasLen) % 4 != 0) {
127 throw new FormatException('''Size of Base 64 characters in Input
128 must be a multiple of 4. Input: $input''');
129 }
130
131 // Count pad characters.
132 int padLength = 0;
133 for (int i = len - 1; i >= 0; i--) {
134 int currentCodeUnit = input.codeUnitAt(i);
135 if (_decodeTable[currentCodeUnit] > 0) break;
136 if (currentCodeUnit == PAD) padLength++;
137 }
138 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength;
139 List<int> out = new List<int>(outputLen);
140
141 for (int i = 0, o = 0; o < outputLen; ) {
142 // Accumulate 4 valid 6 bit Base 64 characters into an int.
143 int x = 0;
144 for (int j = 4; j > 0; ) {
145 int c = _decodeTable[input.codeUnitAt(i++)];
146 if (c >= 0) {
147 x = ((x << 6) & 0xFFFFFF) | c;
148 j--;
149 }
150 }
151 out[o++] = x >> 16;
152 if (o < outputLen) {
153 out[o++] = (x >> 8) & 0xFF;
154 if (o < outputLen) out[o++] = x & 0xFF;
155 }
156 }
157 return out;
158 } 26 }
159
160 } 27 }
OLDNEW
« no previous file with comments | « lib/src/base64.dart ('k') | test/base64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698