Chromium Code Reviews| Index: sdk/lib/crypto/crypto_utils.dart |
| diff --git a/sdk/lib/crypto/crypto_utils.dart b/sdk/lib/crypto/crypto_utils.dart |
| index 4c09dff4b00333c7044870a7d3078b5aae78d29a..d6b1e17df19796b0f33842919bf123ef69b7c302 100644 |
| --- a/sdk/lib/crypto/crypto_utils.dart |
| +++ b/sdk/lib/crypto/crypto_utils.dart |
| @@ -4,25 +4,6 @@ |
| part of dart.crypto; |
| -class _LineWrappingStringBuffer { |
| - _LineWrappingStringBuffer(int this._lineLength) : _sb = new StringBuffer(); |
| - |
| - void write(String s) { |
| - if (_lineLength != null && _currentLineLength == _lineLength) { |
| - _sb.write('\r\n'); |
| - _currentLineLength = 0; |
| - } |
| - _sb.write(s); |
| - _currentLineLength++; |
| - } |
| - |
| - String toString() => _sb.toString(); |
| - |
| - int _lineLength; |
| - StringBuffer _sb; |
| - int _currentLineLength = 0; |
| -} |
| - |
| abstract class _CryptoUtils { |
| static String bytesToHex(List<int> bytes) { |
| var result = new StringBuffer(); |
| @@ -32,45 +13,146 @@ abstract class _CryptoUtils { |
| return result.toString(); |
| } |
| - static String bytesToBase64(List<int> bytes, [int lineLength]) { |
| - final table = |
| - const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
| - 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
| - 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| - 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', |
| - '8', '9', '+', '/' ]; |
| - |
| - var result = new _LineWrappingStringBuffer(lineLength); |
| - |
| - // Encode all full 24-bit blocks. |
| - var i = 0; |
| - for (; (i + 2) < bytes.length; i += 3) { |
| - var b0 = bytes[i] & 0xff; |
| - var b1 = bytes[i + 1] & 0xff; |
| - var b2 = bytes[i + 2] & 0xff; |
| - result.write(table[b0 >> 2]); |
| - result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); |
| - result.write(table[((b1 << 2) | (b2 >> 6)) & 0x3f]); |
| - result.write(table[b2 & 0x3f]); |
| + 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.
|
| + static final int CR = '\r'.codeUnitAt(0); |
| + static final int LF = '\n'.codeUnitAt(0); |
| + |
| + 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.
|
| + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| + |
| + static final String _encodeTableUrlSafe = |
|
floitsch
2013/03/22 17:22:05
ditto.
mdakin1
2013/03/25 17:05:02
Done.
|
| + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| + |
| + // Lookup table used for finding Base 64 alphabet index of a given byte. |
| + // -2 : Outside base64 alphabet |
| + // -1 : \r or \n |
| + // 0 : = (Padding character). |
| + // >0 : base64 alphabet index of given byte. |
| + static const List<int> _decodeTable = |
| + const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, |
| + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2, |
| + -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, |
| + -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, |
| + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ]; |
| + |
| + static String bytesToBase64(List<int> bytes, |
| + [bool urlSafe = false, |
| + 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.
|
| + bool usePadding = true]) { |
| + int len = bytes.length; |
| + if (len == 0) { |
| + return ""; |
| } |
| + final String lookup = urlSafe ? _encodeTable : _encodeTableUrlSafe; |
| + // Size of 24 bit chunks. |
| + final int padLength = len % 3; |
| + final int chunkLength = len - padLength; |
| + // Size of base output. |
| + int outputLen = ((len ~/ 3) * 4) + ((padLength > 0 && usePadding) ? 4 : 0); |
| + // Add extra for line separators. |
| + if (addLineSeparator) { |
| + 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.
|
| + } |
| + List<int> out = new List<int>(outputLen); |
| - // Deal with the last non-full block if any and add padding '='. |
| - if (i == bytes.length - 1) { |
| - var b0 = bytes[i] & 0xff; |
| - result.write(table[b0 >> 2]); |
| - result.write(table[(b0 << 4) & 0x3f]); |
| - result.write('='); |
| - result.write('='); |
| - } else if (i == bytes.length - 2) { |
| - var b0 = bytes[i] & 0xff; |
| - var b1 = bytes[i + 1] & 0xff; |
| - result.write(table[b0 >> 2]); |
| - result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); |
| - result.write(table[(b1 << 2) & 0x3f]); |
| - result.write('='); |
| + // Encode 24 bit chunks. |
| + int j = 0, i = 0, cc = 0; |
| + while (i < chunkLength) { |
| + 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.
|
| + out[j++] = lookup.codeUnitAt(x >> 18); |
| + out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); |
| + out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); |
| + out[j++] = lookup.codeUnitAt(x & 0x3f); |
| + // Add optional line separator for each 76 char output. |
| + if (addLineSeparator && ++cc == 19 && j < outputLen - 2) { |
| + out[j++] = CR; |
| + out[j++] = LF; |
| + cc = 0; |
| + } |
| } |
| - return result.toString(); |
| + // 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.
|
| + if (padLength == 1) { |
| + int x = bytes[i++]; |
| + out[j++] = lookup.codeUnitAt(x >> 2); |
| + out[j++] = lookup.codeUnitAt((x & 0x3) << 4); |
| + if (usePadding) { |
| + out[j++] = PAD; |
| + out[j++] = PAD; |
| + } |
| + } else if (padLength == 2) { |
| + int x = (bytes[i++] << 10) | (bytes[i++] << 2); |
| + out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); |
| + out[j++] = lookup.codeUnitAt((x >> 6) & 0x3f); |
| + out[j++] = lookup.codeUnitAt(x & 0x3F); |
| + if (usePadding) out[j++] = PAD; |
| + } |
| + |
| + return new String.fromCharCodes(out); |
| } |
| + |
| + static List<int> base64StringToBytes(String input, [bool ignoreErrors]) { |
| + int len = input.length; |
| + if (len == 0) { |
| + return new List<int>(0); |
| + } |
| + |
| + // Count '\r', '\n' and illegal characters, |
| + // For illegal characters, if [ignoreErrors] is false, throw an exception. |
| + int extrasLen = 0; |
| + for (int i = 0; i < len; i++) { |
| + int c = _decodeTable[input.codeUnitAt(i)]; |
| + if (c < 0) { |
| + 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.
|
| + extrasLen++; |
| + } 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.
|
| + throw new FormatException('Invalid character: ${input[i]}'); |
| + } |
| + } |
| + } |
| + |
| + 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.
|
| + throw new FormatException('Invalid input.'); |
| + } |
| + |
| + // Count pad characters, ignore illegal characters at the end. |
| + int padLength = 0; |
| + 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.
|
| + if (input.codeUnitAt(i) == PAD) { |
| + padLength++; |
| + } |
| + } |
| + int outputLen = (((len - extrasLen) * 6) >> 3) - padLength; |
| + List<int> out = new List<int>(outputLen); |
| + |
| + for (int i = 0, o = 0; o < outputLen;) { |
| + // Accumulate 4 valid 6 bit base64 characters into an int. |
| + int x = 0; |
| + for (int j = 4; j > 0;) { |
| + int c = _decodeTable[input.codeUnitAt(i++)]; |
| + if (c >= 0) { |
| + 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.
|
| + j--; |
| + } |
| + } |
| + out[o++] = x >> 16; |
| + if (o < outputLen) { |
| + out[o++] = (x >> 8) & 0xFF; |
| + if (o < outputLen) out[o++] = x & 0xFF; |
| + } |
| + } |
| + return out; |
| + } |
| + |
| } |