Base64Utils.uts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. export class Base64 {
  2. private _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  3. encode(input: string) : string {
  4. let output = "";
  5. let chr1 : number, chr2 : number, chr3 : number, enc1 : number, enc2 : number, enc3 : number, enc4 : number;
  6. let i = 0;
  7. input = this._utf8_encode(input);
  8. while (i < input.length) {
  9. chr1 = input.charCodeAt(i++);
  10. chr2 = input.charCodeAt(i++);
  11. chr3 = input.charCodeAt(i++);
  12. enc1 = chr1 >> 2;
  13. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  14. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  15. enc4 = chr3 & 63;
  16. if (isNaN(chr2)) {
  17. enc3 = enc4 = 64;
  18. } else if (isNaN(chr3)) {
  19. enc4 = 64;
  20. }
  21. output = output +
  22. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  23. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  24. }
  25. return output;
  26. }
  27. decode(input: string) : string {
  28. let output = "";
  29. let chr1 : number, chr2 : number, chr3 : number;
  30. let enc1 : number, enc2 : number, enc3 : number, enc4 : number;
  31. let i = 0;
  32. input = input.replace(/[^A-Za-z0-9]/g, "");
  33. while (i < input.length) {
  34. enc1 = this._keyStr.indexOf(input.charAt(i++));
  35. enc2 = this._keyStr.indexOf(input.charAt(i++));
  36. enc3 = this._keyStr.indexOf(input.charAt(i++));
  37. enc4 = this._keyStr.indexOf(input.charAt(i++));
  38. chr1 = (enc1 << 2) | (enc2 >> 4);
  39. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  40. chr3 = ((enc3 & 3) << 6) | enc4;
  41. output = output + String.fromCharCode(chr1);
  42. if (enc3 != 64) {
  43. output = output + String.fromCharCode(chr2);
  44. }
  45. if (enc4 != 64) {
  46. output = output + String.fromCharCode(chr3);
  47. }
  48. }
  49. output = this._utf8_decode(output);
  50. return output;
  51. }
  52. _utf8_encode(string: string) : string {
  53. string = string.replace(/\r\n/g, "\n");
  54. let utftext = "";
  55. for (let n = 0; n < string.length; n++) {
  56. const c = string.charCodeAt(n);
  57. if (c < 128) {
  58. utftext += String.fromCharCode(c);
  59. } else if ((c > 127) && (c < 2048)) {
  60. utftext += String.fromCharCode((c >> 6) | 192);
  61. utftext += String.fromCharCode((c & 63) | 128);
  62. } else {
  63. utftext += String.fromCharCode((c >> 12) | 224);
  64. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  65. utftext += String.fromCharCode((c & 63) | 128);
  66. }
  67. }
  68. return utftext;
  69. }
  70. // private method for UTF-8 decoding
  71. _utf8_decode(utftext: string) : string {
  72. let string = "";
  73. let i = 0;
  74. let c = 0;
  75. let c1 = 0;
  76. let c2 = 0;
  77. while (i < utftext.length) {
  78. c = utftext.charCodeAt(i);
  79. if (c < 128) {
  80. string += String.fromCharCode(c);
  81. i++;
  82. } else if ((c > 191) && (c < 224)) {
  83. c1 = utftext.charCodeAt(i + 1);
  84. string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
  85. i += 2;
  86. } else {
  87. c1 = utftext.charCodeAt(i + 1);
  88. c2 = utftext.charCodeAt(i + 2);
  89. string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
  90. i += 3;
  91. }
  92. }
  93. return string;
  94. }
  95. }