Advertisement
Guest User

Dart Base64String Encode/Decode

a guest
Feb 8th, 2013
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. class Base64String {
  2. static const List<String> _encodingTable = const [
  3. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  4. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  5. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  6. 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  7. '8', '9', '+', '/'];
  8.  
  9. static String encode(String income ) {
  10.  
  11. List<int> data = income.charCodes;
  12.  
  13. List<String> characters = new List<String>();
  14. int i;
  15. for (i = 0; i + 3 <= data.length; i += 3) {
  16. int value = 0;
  17. value |= data[i + 2];
  18. value |= data[i + 1] << 8;
  19. value |= data[i] << 16;
  20. for (int j = 0; j < 4; j++) {
  21. int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
  22. characters.add(_encodingTable[index]);
  23. }
  24. }
  25. // Remainders.
  26. if (i + 2 == data.length) {
  27. int value = 0;
  28. value |= data[i + 1] << 8;
  29. value |= data[i] << 16;
  30. for (int j = 0; j < 3; j++) {
  31. int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
  32. characters.add(_encodingTable[index]);
  33. }
  34. characters.add("=");
  35. } else if (i + 1 == data.length) {
  36. int value = 0;
  37. value |= data[i] << 16;
  38. for (int j = 0; j < 2; j++) {
  39. int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
  40. characters.add(_encodingTable[index]);
  41. }
  42. characters.add("=");
  43. characters.add("=");
  44. }
  45. StringBuffer output = new StringBuffer();
  46. for (i = 0; i < characters.length; i++) {
  47. if (i > 0 && i % 76 == 0) {
  48. output.add("\r\n");
  49. }
  50. output.add(characters[i]);
  51. }
  52. return output.toString();
  53. }
  54.  
  55.  
  56. static String decode(String data) {
  57. List<int> result = new List<int>();
  58. int padCount = 0;
  59. int charCount = 0;
  60. int value = 0;
  61. for (int i = 0; i < data.length; i++) {
  62. int char = data.charCodeAt(i);
  63. if (65 <= char && char <= 90) { // "A" - "Z".
  64. value = (value << 6) | char - 65;
  65. charCount++;
  66. } else if (97 <= char && char <= 122) { // "a" - "z".
  67. value = (value << 6) | char - 97 + 26;
  68. charCount++;
  69. } else if (48 <= char && char <= 57) { // "0" - "9".
  70. value = (value << 6) | char - 48 + 52;
  71. charCount++;
  72. } else if (char == 43) { // "+".
  73. value = (value << 6) | 62;
  74. charCount++;
  75. } else if (char == 47) { // "/".
  76. value = (value << 6) | 63;
  77. charCount++;
  78. } else if (char == 61) { // "=".
  79. value = (value << 6);
  80. charCount++;
  81. padCount++;
  82. }
  83. if (charCount == 4) {
  84. result.add((value & 0xFF0000) >> 16);
  85. if (padCount < 2) {
  86. result.add((value & 0xFF00) >> 8);
  87. }
  88. if (padCount == 0) {
  89. result.add(value & 0xFF);
  90. }
  91. charCount = 0;
  92. value = 0;
  93. }
  94. }
  95.  
  96. return new String.fromCharCodes( result );
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement