| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.crypto; | |
| 6 | |
| 7 abstract class _CryptoUtils { | |
| 8 static String bytesToHex(List<int> bytes) { | |
| 9 var result = new StringBuffer(); | |
| 10 for (var part in bytes) { | |
| 11 result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); | |
| 12 } | |
| 13 return result.toString(); | |
| 14 } | |
| 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, | |
| 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 } | |
| 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 } | |
| 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 | |
| 161 } | |
| OLD | NEW |