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