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