| Index: lib/crypto/crypto_utils.dart
|
| diff --git a/lib/crypto/crypto_utils.dart b/lib/crypto/crypto_utils.dart
|
| index aa364b68bdcc6e4cfec6d2a01c2eef27d1cde8ea..043ab2130abbaa8b46087671fc2e1b351fc34582 100644
|
| --- a/lib/crypto/crypto_utils.dart
|
| +++ b/lib/crypto/crypto_utils.dart
|
| @@ -2,6 +2,25 @@
|
| // for details. All rights reserved. Use of this source code is governed by a
|
| // BSD-style license that can be found in the LICENSE file.
|
|
|
| +class _LineWrappingStringBuffer {
|
| + _LineWrappingStringBuffer(int this._lineLength) : _sb = new StringBuffer();
|
| +
|
| + void add(String s) {
|
| + if (_lineLength !== null && _currentLineLength == _lineLength) {
|
| + _sb.add('\r\n');
|
| + _currentLineLength = 0;
|
| + }
|
| + _sb.add(s);
|
| + _currentLineLength++;
|
| + }
|
| +
|
| + String toString() => _sb.toString();
|
| +
|
| + int _lineLength;
|
| + StringBuffer _sb;
|
| + int _currentLineLength = 0;
|
| +}
|
| +
|
| class _CryptoUtils {
|
| static String bytesToHex(List<int> bytes) {
|
| var result = new StringBuffer();
|
| @@ -10,4 +29,46 @@ 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.add(table[b0 >> 2]);
|
| + result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
|
| + result.add(table[((b1 << 2) | (b2 >> 6)) & 0x3f]);
|
| + result.add(table[b2 & 0x3f]);
|
| + }
|
| +
|
| + // Deal with the last non-full block if any and add padding '='.
|
| + if (i == bytes.length - 1) {
|
| + var b0 = bytes[i] & 0xff;
|
| + result.add(table[b0 >> 2]);
|
| + result.add(table[(b0 << 4) & 0x3f]);
|
| + result.add('=');
|
| + result.add('=');
|
| + } else if (i == bytes.length - 2) {
|
| + var b0 = bytes[i] & 0xff;
|
| + var b1 = bytes[i + 1] & 0xff;
|
| + result.add(table[b0 >> 2]);
|
| + result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
|
| + result.add(table[(b1 << 2) & 0x3f]);
|
| + result.add('=');
|
| + }
|
| +
|
| + return result.toString();
|
| + }
|
| }
|
|
|