| 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 class _LineWrappingStringBuffer { |
| 6 _LineWrappingStringBuffer(int this._lineLength) : _sb = new StringBuffer(); |
| 7 |
| 8 void add(String s) { |
| 9 if (_lineLength !== null && _currentLineLength == _lineLength) { |
| 10 _sb.add('\r\n'); |
| 11 _currentLineLength = 0; |
| 12 } |
| 13 _sb.add(s); |
| 14 _currentLineLength++; |
| 15 } |
| 16 |
| 17 String toString() => _sb.toString(); |
| 18 |
| 19 int _lineLength; |
| 20 StringBuffer _sb; |
| 21 int _currentLineLength = 0; |
| 22 } |
| 23 |
| 5 class _CryptoUtils { | 24 class _CryptoUtils { |
| 6 static String bytesToHex(List<int> bytes) { | 25 static String bytesToHex(List<int> bytes) { |
| 7 var result = new StringBuffer(); | 26 var result = new StringBuffer(); |
| 8 for (var part in bytes) { | 27 for (var part in bytes) { |
| 9 result.add('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); | 28 result.add('${part < 16 ? '0' : ''}${part.toRadixString(16)}'); |
| 10 } | 29 } |
| 11 return result.toString(); | 30 return result.toString(); |
| 12 } | 31 } |
| 32 |
| 33 static String bytesToBase64(List<int> bytes, [int lineLength]) { |
| 34 final table = |
| 35 const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
| 36 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 37 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
| 38 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 39 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', |
| 40 '8', '9', '+', '/' ]; |
| 41 |
| 42 var result = new _LineWrappingStringBuffer(lineLength); |
| 43 |
| 44 // Encode all full 24-bit blocks. |
| 45 var i = 0; |
| 46 for (; (i + 2) < bytes.length; i += 3) { |
| 47 var b0 = bytes[i] & 0xff; |
| 48 var b1 = bytes[i + 1] & 0xff; |
| 49 var b2 = bytes[i + 2] & 0xff; |
| 50 result.add(table[b0 >> 2]); |
| 51 result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); |
| 52 result.add(table[((b1 << 2) | (b2 >> 6)) & 0x3f]); |
| 53 result.add(table[b2 & 0x3f]); |
| 54 } |
| 55 |
| 56 // Deal with the last non-full block if any and add padding '='. |
| 57 if (i == bytes.length - 1) { |
| 58 var b0 = bytes[i] & 0xff; |
| 59 result.add(table[b0 >> 2]); |
| 60 result.add(table[(b0 << 4) & 0x3f]); |
| 61 result.add('='); |
| 62 result.add('='); |
| 63 } else if (i == bytes.length - 2) { |
| 64 var b0 = bytes[i] & 0xff; |
| 65 var b1 = bytes[i + 1] & 0xff; |
| 66 result.add(table[b0 >> 2]); |
| 67 result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]); |
| 68 result.add(table[(b1 << 2) & 0x3f]); |
| 69 result.add('='); |
| 70 } |
| 71 |
| 72 return result.toString(); |
| 73 } |
| 13 } | 74 } |
| OLD | NEW |