base64.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // A base64 implementation for the bcrypt algorithm. This is partly non-standard.
  2. /**
  3. * bcrypt's own non-standard base64 dictionary.
  4. * @type {!Array.<string>}
  5. * @const
  6. * @inner
  7. **/
  8. var BASE64_CODE = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split('');
  9. /**
  10. * @type {!Array.<number>}
  11. * @const
  12. * @inner
  13. **/
  14. var BASE64_INDEX = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  15. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
  17. 1, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, -1, -1, -1, -1,
  18. -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  19. 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1, 28, 29, 30,
  20. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  21. 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1];
  22. /**
  23. * @type {!function(...number):string}
  24. * @inner
  25. */
  26. var stringFromCharCode = String.fromCharCode;
  27. /**
  28. * Encodes a byte array to base64 with up to len bytes of input.
  29. * @param {!Array.<number>} b Byte array
  30. * @param {number} len Maximum input length
  31. * @returns {string}
  32. * @inner
  33. */
  34. function base64_encode(b, len) {
  35. var off = 0,
  36. rs = [],
  37. c1, c2;
  38. if (len <= 0 || len > b.length)
  39. throw Error("Illegal len: "+len);
  40. while (off < len) {
  41. c1 = b[off++] & 0xff;
  42. rs.push(BASE64_CODE[(c1 >> 2) & 0x3f]);
  43. c1 = (c1 & 0x03) << 4;
  44. if (off >= len) {
  45. rs.push(BASE64_CODE[c1 & 0x3f]);
  46. break;
  47. }
  48. c2 = b[off++] & 0xff;
  49. c1 |= (c2 >> 4) & 0x0f;
  50. rs.push(BASE64_CODE[c1 & 0x3f]);
  51. c1 = (c2 & 0x0f) << 2;
  52. if (off >= len) {
  53. rs.push(BASE64_CODE[c1 & 0x3f]);
  54. break;
  55. }
  56. c2 = b[off++] & 0xff;
  57. c1 |= (c2 >> 6) & 0x03;
  58. rs.push(BASE64_CODE[c1 & 0x3f]);
  59. rs.push(BASE64_CODE[c2 & 0x3f]);
  60. }
  61. return rs.join('');
  62. }
  63. /**
  64. * Decodes a base64 encoded string to up to len bytes of output.
  65. * @param {string} s String to decode
  66. * @param {number} len Maximum output length
  67. * @returns {!Array.<number>}
  68. * @inner
  69. */
  70. function base64_decode(s, len) {
  71. var off = 0,
  72. slen = s.length,
  73. olen = 0,
  74. rs = [],
  75. c1, c2, c3, c4, o, code;
  76. if (len <= 0)
  77. throw Error("Illegal len: "+len);
  78. while (off < slen - 1 && olen < len) {
  79. code = s.charCodeAt(off++);
  80. c1 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
  81. code = s.charCodeAt(off++);
  82. c2 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
  83. if (c1 == -1 || c2 == -1)
  84. break;
  85. o = (c1 << 2) >>> 0;
  86. o |= (c2 & 0x30) >> 4;
  87. rs.push(stringFromCharCode(o));
  88. if (++olen >= len || off >= slen)
  89. break;
  90. code = s.charCodeAt(off++);
  91. c3 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
  92. if (c3 == -1)
  93. break;
  94. o = ((c2 & 0x0f) << 4) >>> 0;
  95. o |= (c3 & 0x3c) >> 2;
  96. rs.push(stringFromCharCode(o));
  97. if (++olen >= len || off >= slen)
  98. break;
  99. code = s.charCodeAt(off++);
  100. c4 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
  101. o = ((c3 & 0x03) << 6) >>> 0;
  102. o |= c4;
  103. rs.push(stringFromCharCode(o));
  104. ++olen;
  105. }
  106. var res = [];
  107. for (off = 0; off<olen; off++)
  108. res.push(rs[off].charCodeAt(0));
  109. return res;
  110. }