Chromium Code Reviews| 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.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); |
|
floitsch
2013/03/22 17:22:05
I would make this a const. Even if that means that
mdakin1
2013/03/25 17:05:02
Done.
| |
| 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 String _encodeTable = |
|
floitsch
2013/03/22 17:22:05
const, even though it shouldn't matter much.
mdakin1
2013/03/25 17:05:02
Done.
| |
| 21 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
| 45 | 22 |
| 46 // Encode all full 24-bit blocks. | 23 static final String _encodeTableUrlSafe = |
|
floitsch
2013/03/22 17:22:05
ditto.
mdakin1
2013/03/25 17:05:02
Done.
| |
| 47 var i = 0; | 24 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 48 for (; (i + 2) < bytes.length; i += 3) { | 25 |
| 49 var b0 = bytes[i] & 0xff; | 26 // Lookup table used for finding Base 64 alphabet index of a given byte. |
| 50 var b1 = bytes[i + 1] & 0xff; | 27 // -2 : Outside base64 alphabet |
| 51 var b2 = bytes[i + 2] & 0xff; | 28 // -1 : \r or \n |
| 52 result.write(table[b0 >> 2]); | 29 // 0 : = (Padding character). |
| 53 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); | 30 // >0 : base64 alphabet index of given byte. |
| 54 result.write(table[((b1 << 2) | (b2 >> 6)) & 0x3f]); | 31 static const List<int> _decodeTable = |
| 55 result.write(table[b2 & 0x3f]); | 32 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, |
| 33 -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, 62, -2, 62, -2, 63, | |
| 35 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2, | |
| 36 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
| 37 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, | |
| 38 -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
| 39 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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, -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 | |
| 49 static String bytesToBase64(List<int> bytes, | |
| 50 [bool urlSafe = false, | |
| 51 bool addLineSeparator = false, | |
|
floitsch
2013/03/22 17:22:05
nit: add one space in front of the "bool addLi..."
mdakin1
2013/03/25 17:05:02
Done.
| |
| 52 bool usePadding = true]) { | |
| 53 int len = bytes.length; | |
| 54 if (len == 0) { | |
| 55 return ""; | |
| 56 } | |
| 57 final String lookup = urlSafe ? _encodeTable : _encodeTableUrlSafe; | |
| 58 // Size of 24 bit chunks. | |
| 59 final int padLength = len % 3; | |
| 60 final int chunkLength = len - padLength; | |
| 61 // Size of base output. | |
| 62 int outputLen = ((len ~/ 3) * 4) + ((padLength > 0 && usePadding) ? 4 : 0); | |
| 63 // Add extra for line separators. | |
| 64 if (addLineSeparator) { | |
| 65 outputLen += ((outputLen - 1) ~/ 76) << 1; | |
|
floitsch
2013/03/22 17:22:05
Maybe put 76 in a constant?
mdakin1
2013/03/25 17:05:02
Done.
| |
| 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) | (bytes[i++] << 8) | bytes[i++]; | |
|
floitsch
2013/03/22 17:22:05
I wonder if you could help the VM and dart2js if y
mdakin1
2013/03/25 17:05:02
Done.
I added a 0xFFFFFF mask per << operation, h
floitsch
2013/03/25 17:45:37
Yes. that's what I wanted.
| |
| 73 out[j++] = lookup.codeUnitAt(x >> 18); | |
| 74 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); | |
| 75 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); | |
| 76 out[j++] = lookup.codeUnitAt(x & 0x3f); | |
| 77 // Add optional line separator for each 76 char output. | |
| 78 if (addLineSeparator && ++cc == 19 && j < outputLen - 2) { | |
| 79 out[j++] = CR; | |
| 80 out[j++] = LF; | |
| 81 cc = 0; | |
| 82 } | |
| 56 } | 83 } |
| 57 | 84 |
| 58 // Deal with the last non-full block if any and add padding '='. | 85 // If input length if not a multiple of 3, encode remaining add padding. |
|
floitsch
2013/03/22 17:22:05
encode the remaining bytes and add padding.
mdakin1
2013/03/25 17:05:02
Done.
| |
| 59 if (i == bytes.length - 1) { | 86 if (padLength == 1) { |
| 60 var b0 = bytes[i] & 0xff; | 87 int x = bytes[i++]; |
| 61 result.write(table[b0 >> 2]); | 88 out[j++] = lookup.codeUnitAt(x >> 2); |
| 62 result.write(table[(b0 << 4) & 0x3f]); | 89 out[j++] = lookup.codeUnitAt((x & 0x3) << 4); |
| 63 result.write('='); | 90 if (usePadding) { |
| 64 result.write('='); | 91 out[j++] = PAD; |
| 65 } else if (i == bytes.length - 2) { | 92 out[j++] = PAD; |
| 66 var b0 = bytes[i] & 0xff; | 93 } |
| 67 var b1 = bytes[i + 1] & 0xff; | 94 } else if (padLength == 2) { |
| 68 result.write(table[b0 >> 2]); | 95 int x = (bytes[i++] << 10) | (bytes[i++] << 2); |
| 69 result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); | 96 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); |
| 70 result.write(table[(b1 << 2) & 0x3f]); | 97 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3f); |
| 71 result.write('='); | 98 out[j++] = lookup.codeUnitAt(x & 0x3F); |
| 99 if (usePadding) out[j++] = PAD; | |
| 72 } | 100 } |
| 73 | 101 |
| 74 return result.toString(); | 102 return new String.fromCharCodes(out); |
| 75 } | 103 } |
| 104 | |
| 105 static List<int> base64StringToBytes(String input, [bool ignoreErrors]) { | |
| 106 int len = input.length; | |
| 107 if (len == 0) { | |
| 108 return new List<int>(0); | |
| 109 } | |
| 110 | |
| 111 // Count '\r', '\n' and illegal characters, | |
| 112 // For illegal characters, if [ignoreErrors] is false, throw an exception. | |
| 113 int extrasLen = 0; | |
| 114 for (int i = 0; i < len; i++) { | |
| 115 int c = _decodeTable[input.codeUnitAt(i)]; | |
| 116 if (c < 0) { | |
| 117 if (c == -1) { | |
|
floitsch
2013/03/22 17:22:05
indentation. (2chars)
mdakin1
2013/03/25 17:05:02
Done.
mdakin1
2013/03/25 17:05:02
Done.
| |
| 118 extrasLen++; | |
| 119 } else if (c == -2 && !ignoreErrors) { | |
|
floitsch
2013/03/22 17:22:05
Change this if to:
if (c < 0) {
extrasLen++;
i
mdakin1
2013/03/25 17:05:02
Done.
| |
| 120 throw new FormatException('Invalid character: ${input[i]}'); | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 if ((len - extrasLen) % 4 != 0) { | |
|
floitsch
2013/03/22 17:22:05
Needs change above to work.
Add test for it.
mdakin1
2013/03/25 17:05:02
Done.
| |
| 126 throw new FormatException('Invalid input.'); | |
| 127 } | |
| 128 | |
| 129 // Count pad characters, ignore illegal characters at the end. | |
| 130 int padLength = 0; | |
| 131 for (int i = len; i > 1 && _decodeTable[input.codeUnitAt(--i)] <= 0;) { | |
|
floitsch
2013/03/22 17:22:05
I prefer:
for (int i = len - 1; i >= 0; i--) {
i
mdakin1
2013/03/25 17:05:02
Done.
Cleaner, thanks.
| |
| 132 if (input.codeUnitAt(i) == PAD) { | |
| 133 padLength++; | |
| 134 } | |
| 135 } | |
| 136 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength; | |
| 137 List<int> out = new List<int>(outputLen); | |
| 138 | |
| 139 for (int i = 0, o = 0; o < outputLen;) { | |
| 140 // Accumulate 4 valid 6 bit base64 characters into an int. | |
| 141 int x = 0; | |
| 142 for (int j = 4; j > 0;) { | |
| 143 int c = _decodeTable[input.codeUnitAt(i++)]; | |
| 144 if (c >= 0) { | |
| 145 x = (x << 6) | c; | |
|
floitsch
2013/03/22 17:22:05
I would bit-and to help the vm and dart2js.
Otherw
mdakin1
2013/03/25 17:05:02
Done.
| |
| 146 j--; | |
| 147 } | |
| 148 } | |
| 149 out[o++] = x >> 16; | |
| 150 if (o < outputLen) { | |
| 151 out[o++] = (x >> 8) & 0xFF; | |
| 152 if (o < outputLen) out[o++] = x & 0xFF; | |
| 153 } | |
| 154 } | |
| 155 return out; | |
| 156 } | |
| 157 | |
| 76 } | 158 } |
| OLD | NEW |