| 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 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) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 while (i < chunkLength) { | 71 while (i < chunkLength) { |
| 72 int x = ((bytes[i++] << 16) & 0xFFFFFF) | | 72 int x = ((bytes[i++] << 16) & 0xFFFFFF) | |
| 73 ((bytes[i++] << 8) & 0xFFFFFF) | | 73 ((bytes[i++] << 8) & 0xFFFFFF) | |
| 74 bytes[i++]; | 74 bytes[i++]; |
| 75 out[j++] = lookup.codeUnitAt(x >> 18); | 75 out[j++] = lookup.codeUnitAt(x >> 18); |
| 76 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); | 76 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); |
| 77 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); | 77 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); |
| 78 out[j++] = lookup.codeUnitAt(x & 0x3f); | 78 out[j++] = lookup.codeUnitAt(x & 0x3f); |
| 79 // Add optional line separator for each 76 char output. | 79 // Add optional line separator for each 76 char output. |
| 80 if (addLineSeparator && ++c == 19 && j < outputLen - 2) { | 80 if (addLineSeparator && ++c == 19 && j < outputLen - 2) { |
| 81 out[j++] = CR; | 81 out[j++] = CR; |
| 82 out[j++] = LF; | 82 out[j++] = LF; |
| 83 c = 0; | 83 c = 0; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 // If input length if not a multiple of 3, encode remaining bytes and | 87 // If input length if not a multiple of 3, encode remaining bytes and |
| 88 // add padding. | 88 // add padding. |
| 89 if (remainderLength == 1) { | 89 if (remainderLength == 1) { |
| 90 int x = bytes[i]; | 90 int x = bytes[i]; |
| 91 out[j++] = lookup.codeUnitAt(x >> 2); | 91 out[j++] = lookup.codeUnitAt(x >> 2); |
| 92 out[j++] = lookup.codeUnitAt((x << 4) & 0x3F); | 92 out[j++] = lookup.codeUnitAt((x << 4) & 0x3F); |
| 93 out[j++] = PAD; | 93 out[j++] = PAD; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 110 return new List<int>(0); | 110 return new List<int>(0); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Count '\r', '\n' and illegal characters, For illegal characters, | 113 // Count '\r', '\n' and illegal characters, For illegal characters, |
| 114 // throw an exception. | 114 // throw an exception. |
| 115 int extrasLen = 0; | 115 int extrasLen = 0; |
| 116 for (int i = 0; i < len; i++) { | 116 for (int i = 0; i < len; i++) { |
| 117 int c = _decodeTable[input.codeUnitAt(i)]; | 117 int c = _decodeTable[input.codeUnitAt(i)]; |
| 118 if (c < 0) { | 118 if (c < 0) { |
| 119 extrasLen++; | 119 extrasLen++; |
| 120 if(c == -2) { | 120 if (c == -2) { |
| 121 throw new FormatException('Invalid character: ${input[i]}'); | 121 throw new FormatException('Invalid character: ${input[i]}'); |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 | 125 |
| 126 if ((len - extrasLen) % 4 != 0) { | 126 if ((len - extrasLen) % 4 != 0) { |
| 127 throw new FormatException('''Size of Base 64 characters in Input | 127 throw new FormatException('''Size of Base 64 characters in Input |
| 128 must be a multiple of 4. Input: $input'''); | 128 must be a multiple of 4. Input: $input'''); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Count pad characters. | 131 // Count pad characters. |
| 132 int padLength = 0; | 132 int padLength = 0; |
| 133 for (int i = len - 1; i >= 0; i--) { | 133 for (int i = len - 1; i >= 0; i--) { |
| 134 int currentCodeUnit = input.codeUnitAt(i); | 134 int currentCodeUnit = input.codeUnitAt(i); |
| 135 if (_decodeTable[currentCodeUnit] > 0) break; | 135 if (_decodeTable[currentCodeUnit] > 0) break; |
| 136 if (currentCodeUnit == PAD) padLength++; | 136 if (currentCodeUnit == PAD) padLength++; |
| 137 } | 137 } |
| 138 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength; | 138 int outputLen = (((len - extrasLen) * 6) >> 3) - padLength; |
| 139 List<int> out = new List<int>(outputLen); | 139 List<int> out = new List<int>(outputLen); |
| 140 | 140 |
| 141 for (int i = 0, o = 0; o < outputLen;) { | 141 for (int i = 0, o = 0; o < outputLen; ) { |
| 142 // Accumulate 4 valid 6 bit Base 64 characters into an int. | 142 // Accumulate 4 valid 6 bit Base 64 characters into an int. |
| 143 int x = 0; | 143 int x = 0; |
| 144 for (int j = 4; j > 0;) { | 144 for (int j = 4; j > 0; ) { |
| 145 int c = _decodeTable[input.codeUnitAt(i++)]; | 145 int c = _decodeTable[input.codeUnitAt(i++)]; |
| 146 if (c >= 0) { | 146 if (c >= 0) { |
| 147 x = ((x << 6) & 0xFFFFFF) | c; | 147 x = ((x << 6) & 0xFFFFFF) | c; |
| 148 j--; | 148 j--; |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 out[o++] = x >> 16; | 151 out[o++] = x >> 16; |
| 152 if (o < outputLen) { | 152 if (o < outputLen) { |
| 153 out[o++] = (x >> 8) & 0xFF; | 153 out[o++] = (x >> 8) & 0xFF; |
| 154 if (o < outputLen) out[o++] = x & 0xFF; | 154 if (o < outputLen) out[o++] = x & 0xFF; |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 return out; | 157 return out; |
| 158 } | 158 } |
| 159 | 159 |
| 160 } | 160 } |
| OLD | NEW |