|
| 1 | +/* eslint-disable camelcase */ |
| 2 | +const md5 = (s: string) => { |
| 3 | + function md5cycle(x: Int32Array, k: any[]) { |
| 4 | + let a = x[0]; let b = x[1]; let c = x[2]; let d = x[3] |
| 5 | + |
| 6 | + a = ff(a, b, c, d, k[0], 7, -680876936) |
| 7 | + d = ff(d, a, b, c, k[1], 12, -389564586) |
| 8 | + c = ff(c, d, a, b, k[2], 17, 606105819) |
| 9 | + b = ff(b, c, d, a, k[3], 22, -1044525330) |
| 10 | + a = ff(a, b, c, d, k[4], 7, -176418897) |
| 11 | + d = ff(d, a, b, c, k[5], 12, 1200080426) |
| 12 | + c = ff(c, d, a, b, k[6], 17, -1473231341) |
| 13 | + b = ff(b, c, d, a, k[7], 22, -45705983) |
| 14 | + a = ff(a, b, c, d, k[8], 7, 1770035416) |
| 15 | + d = ff(d, a, b, c, k[9], 12, -1958414417) |
| 16 | + c = ff(c, d, a, b, k[10], 17, -42063) |
| 17 | + b = ff(b, c, d, a, k[11], 22, -1990404162) |
| 18 | + a = ff(a, b, c, d, k[12], 7, 1804603682) |
| 19 | + d = ff(d, a, b, c, k[13], 12, -40341101) |
| 20 | + c = ff(c, d, a, b, k[14], 17, -1502002290) |
| 21 | + b = ff(b, c, d, a, k[15], 22, 1236535329) |
| 22 | + |
| 23 | + a = gg(a, b, c, d, k[1], 5, -165796510) |
| 24 | + d = gg(d, a, b, c, k[6], 9, -1069501632) |
| 25 | + c = gg(c, d, a, b, k[11], 14, 643717713) |
| 26 | + b = gg(b, c, d, a, k[0], 20, -373897302) |
| 27 | + a = gg(a, b, c, d, k[5], 5, -701558691) |
| 28 | + d = gg(d, a, b, c, k[10], 9, 38016083) |
| 29 | + c = gg(c, d, a, b, k[15], 14, -660478335) |
| 30 | + b = gg(b, c, d, a, k[4], 20, -405537848) |
| 31 | + a = gg(a, b, c, d, k[9], 5, 568446438) |
| 32 | + d = gg(d, a, b, c, k[14], 9, -1019803690) |
| 33 | + c = gg(c, d, a, b, k[3], 14, -187363961) |
| 34 | + b = gg(b, c, d, a, k[8], 20, 1163531501) |
| 35 | + a = gg(a, b, c, d, k[13], 5, -1444681467) |
| 36 | + d = gg(d, a, b, c, k[2], 9, -51403784) |
| 37 | + c = gg(c, d, a, b, k[7], 14, 1735328473) |
| 38 | + b = gg(b, c, d, a, k[12], 20, -1926607734) |
| 39 | + |
| 40 | + a = hh(a, b, c, d, k[5], 4, -378558) |
| 41 | + d = hh(d, a, b, c, k[8], 11, -2022574463) |
| 42 | + c = hh(c, d, a, b, k[11], 16, 1839030562) |
| 43 | + b = hh(b, c, d, a, k[14], 23, -35309556) |
| 44 | + a = hh(a, b, c, d, k[1], 4, -1530992060) |
| 45 | + d = hh(d, a, b, c, k[4], 11, 1272893353) |
| 46 | + c = hh(c, d, a, b, k[7], 16, -155497632) |
| 47 | + b = hh(b, c, d, a, k[10], 23, -1094730640) |
| 48 | + a = hh(a, b, c, d, k[13], 4, 681279174) |
| 49 | + d = hh(d, a, b, c, k[0], 11, -358537222) |
| 50 | + c = hh(c, d, a, b, k[3], 16, -722521979) |
| 51 | + b = hh(b, c, d, a, k[6], 23, 76029189) |
| 52 | + a = hh(a, b, c, d, k[9], 4, -640364487) |
| 53 | + d = hh(d, a, b, c, k[12], 11, -421815835) |
| 54 | + c = hh(c, d, a, b, k[15], 16, 530742520) |
| 55 | + b = hh(b, c, d, a, k[2], 23, -995338651) |
| 56 | + |
| 57 | + a = ii(a, b, c, d, k[0], 6, -198630844) |
| 58 | + d = ii(d, a, b, c, k[7], 10, 1126891415) |
| 59 | + c = ii(c, d, a, b, k[14], 15, -1416354905) |
| 60 | + b = ii(b, c, d, a, k[5], 21, -57434055) |
| 61 | + a = ii(a, b, c, d, k[12], 6, 1700485571) |
| 62 | + d = ii(d, a, b, c, k[3], 10, -1894986606) |
| 63 | + c = ii(c, d, a, b, k[10], 15, -1051523) |
| 64 | + b = ii(b, c, d, a, k[1], 21, -2054922799) |
| 65 | + a = ii(a, b, c, d, k[8], 6, 1873313359) |
| 66 | + d = ii(d, a, b, c, k[15], 10, -30611744) |
| 67 | + c = ii(c, d, a, b, k[6], 15, -1560198380) |
| 68 | + b = ii(b, c, d, a, k[13], 21, 1309151649) |
| 69 | + a = ii(a, b, c, d, k[4], 6, -145523070) |
| 70 | + d = ii(d, a, b, c, k[11], 10, -1120210379) |
| 71 | + c = ii(c, d, a, b, k[2], 15, 718787259) |
| 72 | + b = ii(b, c, d, a, k[9], 21, -343485551) |
| 73 | + |
| 74 | + x[0] = add32(a, x[0]) |
| 75 | + x[1] = add32(b, x[1]) |
| 76 | + x[2] = add32(c, x[2]) |
| 77 | + x[3] = add32(d, x[3]) |
| 78 | + } |
| 79 | + |
| 80 | + function cmn(q: number, a: number, b: any, x: any, s: number, t: any) { |
| 81 | + a = add32(add32(a, q), add32(x, t)) |
| 82 | + return add32((a << s) | (a >>> (32 - s)), b) |
| 83 | + } |
| 84 | + |
| 85 | + function ff(a: any, b: number, c: number, d: number, x: any, s: number, t: number) { |
| 86 | + return cmn((b & c) | ((~b) & d), a, b, x, s, t) |
| 87 | + } |
| 88 | + |
| 89 | + function gg(a: any, b: number, c: number, d: number, x: any, s: number, t: number) { |
| 90 | + return cmn((b & d) | (c & (~d)), a, b, x, s, t) |
| 91 | + } |
| 92 | + |
| 93 | + function hh(a: any, b: number, c: number, d: number, x: any, s: number, t: number) { |
| 94 | + return cmn(b ^ c ^ d, a, b, x, s, t) |
| 95 | + } |
| 96 | + |
| 97 | + function ii(a: any, b: number, c: number, d: number, x: any, s: number, t: number) { |
| 98 | + return cmn(c ^ (b | (~d)), a, b, x, s, t) |
| 99 | + } |
| 100 | + |
| 101 | + function md51(s: string) { |
| 102 | + const n = s.length |
| 103 | + const state = new Int32Array([1732584193, -271733879, -1732584194, 271733878]); let i |
| 104 | + for (i = 64; i <= s.length; i += 64) { |
| 105 | + md5cycle(state, md5blk(s.substring(i - 64, i))) |
| 106 | + } |
| 107 | + s = s.substring(i - 64) |
| 108 | + const tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 109 | + for (i = 0; i < s.length; i++) { tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3) } |
| 110 | + tail[i >> 2] |= 0x80 << ((i % 4) << 3) |
| 111 | + if (i > 55) { |
| 112 | + md5cycle(state, tail) |
| 113 | + for (i = 0; i < 16; i++) tail[i] = 0 |
| 114 | + } |
| 115 | + tail[14] = n * 8 |
| 116 | + md5cycle(state, tail) |
| 117 | + return state |
| 118 | + } |
| 119 | + |
| 120 | + /* there needs to be support for Unicode here, |
| 121 | + * unless we pretend that we can redefine the MD-5 |
| 122 | + * algorithm for multi-byte characters (perhaps |
| 123 | + * by adding every four 16-bit characters and |
| 124 | + * shortening the sum to 32 bits). Otherwise |
| 125 | + * I suggest performing MD-5 as if every character |
| 126 | + * was two bytes--e.g., 0040 0025 = @%--but then |
| 127 | + * how will an ordinary MD-5 sum be matched? |
| 128 | + * There is no way to standardize text to something |
| 129 | + * like UTF-8 before transformation; speed cost is |
| 130 | + * utterly prohibitive. The JavaScript standard |
| 131 | + * itself needs to look at this: it should start |
| 132 | + * providing access to strings as preformed UTF-8 |
| 133 | + * 8-bit unsigned value arrays. |
| 134 | + */ |
| 135 | + function md5blk(s: string) { /* I figured global was faster. */ |
| 136 | + const md5blks = [] as number[]; let i /* Andy King said do it this way. */ |
| 137 | + for (i = 0; i < 64; i += 4) { |
| 138 | + md5blks[i >> 2] = s.charCodeAt(i) + |
| 139 | + (s.charCodeAt(i + 1) << 8) + |
| 140 | + (s.charCodeAt(i + 2) << 16) + |
| 141 | + (s.charCodeAt(i + 3) << 24) |
| 142 | + } |
| 143 | + return md5blks |
| 144 | + } |
| 145 | + |
| 146 | + /* this function is much faster, |
| 147 | + so if possible we use it. Some IEs |
| 148 | + are the only ones I know of that |
| 149 | + need the idiotic second function, |
| 150 | + generated by an if clause. */ |
| 151 | + |
| 152 | + function add32(a: number, b: number) { |
| 153 | + return (a + b) & 0xFFFFFFFF |
| 154 | + } |
| 155 | + |
| 156 | + return md51(s) |
| 157 | +} |
| 158 | + |
| 159 | +export function getOfflineUUID(username: string) { |
| 160 | + const md5Bytes = md5(`OfflinePlayer:${username}`) |
| 161 | + md5Bytes[6] &= 0x0f /* clear version */ |
| 162 | + md5Bytes[6] |= 0x30 /* set to version 3 */ |
| 163 | + md5Bytes[8] &= 0x3f /* clear variant */ |
| 164 | + md5Bytes[8] |= 0x80 /* set to IETF variant */ |
| 165 | + |
| 166 | + const hex_chr = '0123456789abcdef'.split('') |
| 167 | + |
| 168 | + function rhex(n: number) { |
| 169 | + let s = ''; let j = 0 |
| 170 | + for (; j < 4; j++) { |
| 171 | + s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + |
| 172 | + hex_chr[(n >> (j * 8)) & 0x0F] |
| 173 | + } |
| 174 | + return s |
| 175 | + } |
| 176 | + |
| 177 | + function hex(x: Int32Array) { |
| 178 | + const reuslt = new Array(4) as string[] |
| 179 | + for (let i = 0; i < x.length; i++) { reuslt[i] = rhex(x[i]) } |
| 180 | + return reuslt.join('') |
| 181 | + } |
| 182 | + |
| 183 | + return hex(md5Bytes).replace(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/, '$1-$2-$3-$4-$5') |
| 184 | +} |
0 commit comments