| 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..e08bbddc9245fa137ce0f6508b3fa780315e76a8 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,149 @@ 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 const int PAD = 61; // '='
|
| + static const int CR = 13; // '\r'
|
| + static const int LF = 10; // '\n'
|
| + static const int LINE_LENGTH = 76;
|
| +
|
| + static const String _encodeTable =
|
| + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
| +
|
| + static const String _encodeTableUrlSafe =
|
| + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
| +
|
| + // Lookup table used for finding Base 64 alphabet index of a given byte.
|
| + // -2 : Outside Base 64 alphabet.
|
| + // -1 : '\r' or '\n'
|
| + // 0 : = (Padding character).
|
| + // >0 : Base 64 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]) {
|
| + int len = bytes.length;
|
| + if (len == 0) {
|
| + return "";
|
| }
|
| + final String lookup = urlSafe ? _encodeTableUrlSafe : _encodeTable;
|
| + // Size of 24 bit chunks.
|
| + final int remainderLength = len.remainder(3);
|
| + final int chunkLength = len - remainderLength;
|
| + // Size of base output.
|
| + int outputLen = ((len ~/ 3) * 4) + ((remainderLength > 0) ? 4 : 0);
|
| + // Add extra for line separators.
|
| + if (addLineSeparator) {
|
| + outputLen += ((outputLen - 1) ~/ LINE_LENGTH) << 1;
|
| + }
|
| + 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, c = 0;
|
| + while (i < chunkLength) {
|
| + int x = ((bytes[i++] << 16) & 0xFFFFFF) |
|
| + ((bytes[i++] << 8) & 0xFFFFFF) |
|
| + bytes[i++];
|
| + 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 && ++c == 19 && j < outputLen - 2) {
|
| + out[j++] = CR;
|
| + out[j++] = LF;
|
| + c = 0;
|
| + }
|
| }
|
|
|
| - return result.toString();
|
| + // If input length if not a multiple of 3, encode remaining bytes and
|
| + // add padding.
|
| + if (remainderLength == 1) {
|
| + int x = bytes[i];
|
| + out[j++] = lookup.codeUnitAt(x >> 2);
|
| + out[j++] = lookup.codeUnitAt((x << 4) & 0x3F);
|
| + out[j++] = PAD;
|
| + out[j++] = PAD;
|
| + } else if (remainderLength == 2) {
|
| + int x = bytes[i];
|
| + int y = bytes[i + 1];
|
| + out[j++] = lookup.codeUnitAt(x >> 2);
|
| + out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F);
|
| + out[j++] = lookup.codeUnitAt((y << 2) & 0x3F);
|
| + out[j++] = PAD;
|
| + }
|
| +
|
| + return new String.fromCharCodes(out);
|
| }
|
| +
|
| + static List<int> base64StringToBytes(String input,
|
| + [bool ignoreInvalidCharacters = true]) {
|
| + int len = input.length;
|
| + if (len == 0) {
|
| + return new List<int>(0);
|
| + }
|
| +
|
| + // Count '\r', '\n' and illegal characters, For illegal characters,
|
| + // if [ignoreInvalidCharacters] is false, throw an exception.
|
| + int extrasLen = 0;
|
| + for (int i = 0; i < len; i++) {
|
| + int c = _decodeTable[input.codeUnitAt(i)];
|
| + if (c < 0) {
|
| + extrasLen++;
|
| + if(c == -2 && !ignoreInvalidCharacters) {
|
| + throw new FormatException('Invalid character: ${input[i]}');
|
| + }
|
| + }
|
| + }
|
| +
|
| + if ((len - extrasLen) % 4 != 0) {
|
| + throw new FormatException('''Size of Base 64 characters in Input
|
| + must be a multiple of 4. Input: $input''');
|
| + }
|
| +
|
| + // Count pad characters, ignore illegal characters at the end.
|
| + int padLength = 0;
|
| + for (int i = len - 1; i >= 0; i--) {
|
| + int currentCodeUnit = input.codeUnitAt(i);
|
| + if (_decodeTable[currentCodeUnit] > 0) break;
|
| + if (currentCodeUnit == 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 Base 64 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) & 0xFFFFFF) | c;
|
| + j--;
|
| + }
|
| + }
|
| + out[o++] = x >> 16;
|
| + if (o < outputLen) {
|
| + out[o++] = (x >> 8) & 0xFF;
|
| + if (o < outputLen) out[o++] = x & 0xFF;
|
| + }
|
| + }
|
| + return out;
|
| + }
|
| +
|
| }
|
|
|