Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /**
  2. * Class to convert from and to base64 encoded strings.
  3. */
  4. class Base64 {
  5. private PADCHAR: string = '=';
  6. private ALPHA: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  7.  
  8. private getByte(s: string, i: number): number {
  9. const x = s.charCodeAt(i);
  10. return x;
  11. }
  12.  
  13. private getByte64(s: string, i: number): number {
  14. const idx = this.ALPHA.indexOf(s.charAt(i));
  15. return idx;
  16. }
  17.  
  18. /**
  19. * Convert a base64 encoded string to a readable string.
  20. * @param s
  21. */
  22. public decode(s: string): string {
  23. let pads = 0,
  24. i, b10, imax = s.length,
  25. x = [];
  26.  
  27. s = String(s);
  28.  
  29. if (imax === 0) {
  30. return s;
  31. }
  32.  
  33. if (s.charAt(imax - 1) === this.PADCHAR) {
  34. pads = 1;
  35. if (s.charAt(imax - 2) === this.PADCHAR) {
  36. pads = 2;
  37. }
  38. imax -= 4;
  39. }
  40.  
  41. for (i = 0; i < imax; i += 4) {
  42. b10 = (this.getByte64(s, i) << 18) | (this.getByte64(s, i + 1) << 12) | (this.getByte64(s, i + 2) << 6) | this.getByte64(s, i + 3);
  43. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 255, b10 & 255));
  44. }
  45.  
  46. switch (pads) {
  47. case 1:
  48. b10 = (this.getByte64(s, i) << 18) | (this.getByte64(s, i + 1) << 12) | (this.getByte64(s, i + 2) << 6);
  49. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 255));
  50. break;
  51. case 2:
  52. b10 = (this.getByte64(s, i) << 18) | (this.getByte64(s, i + 1) << 12);
  53. x.push(String.fromCharCode(b10 >> 16));
  54. break;
  55. }
  56.  
  57. return x.join('');
  58. }
  59.  
  60. /**
  61. * Convert a string to a base64 encoded string.
  62. * @param s
  63. */
  64. public encode(s: string): string {
  65. s = String(s);
  66.  
  67. let i, b10, x = [],
  68. imax = s.length - s.length % 3;
  69.  
  70. if (s.length === 0) {
  71. return s;
  72. }
  73.  
  74. for (i = 0; i < imax; i += 3) {
  75. b10 = (this.getByte(s, i) << 16) | (this.getByte(s, i + 1) << 8) | this.getByte(s, i + 2);
  76. x.push(this.ALPHA.charAt(b10 >> 18));
  77. x.push(this.ALPHA.charAt((b10 >> 12) & 63));
  78. x.push(this.ALPHA.charAt((b10 >> 6) & 63));
  79. x.push(this.ALPHA.charAt(b10 & 63));
  80. }
  81.  
  82. switch (s.length - imax) {
  83. case 1:
  84. b10 = this.getByte(s, i) << 16;
  85. x.push(this.ALPHA.charAt(b10 >> 18) + this.ALPHA.charAt((b10 >> 12) & 63) + this.PADCHAR + this.PADCHAR);
  86. break;
  87. case 2:
  88. b10 = (this.getByte(s, i) << 16) | (this.getByte(s, i + 1) << 8);
  89. x.push(this.ALPHA.charAt(b10 >> 18) + this.ALPHA.charAt((b10 >> 12) & 63) + this.ALPHA.charAt((b10 >> 6) & 63) + this.PADCHAR);
  90. break;
  91. }
  92.  
  93. return x.join('');
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement