* Hashes message using MD5 Cryptographic Hash Function * * @see * For more info: https://en.wikipedia.org/wiki/MD5 * * @param {Uint8Array} message - message to hash * @return {Uint8Array} - message digest (hash value)
(message)
| 151 | * @return {Uint8Array} - message digest (hash value) |
| 152 | */ |
| 153 | function MD5(message) { |
| 154 | // Initialize variables: |
| 155 | let [a0, b0, c0, d0] = [ |
| 156 | 0x67452301 >>> 0, |
| 157 | 0xefcdab89 >>> 0, |
| 158 | 0x98badcfe >>> 0, |
| 159 | 0x10325476 >>> 0 |
| 160 | ] |
| 161 | |
| 162 | // pre-process message and split into 512 bit chunks |
| 163 | const words = Array.from(preProcess(message)) |
| 164 | const chunks = chunkify(words, 16) |
| 165 | |
| 166 | chunks.forEach(function (chunk, _) { |
| 167 | // initialize variables for this chunk |
| 168 | let [A, B, C, D] = [a0, b0, c0, d0] |
| 169 | |
| 170 | for (let i = 0; i < 64; i++) { |
| 171 | let [F, g] = [0, 0] |
| 172 | |
| 173 | if (i <= 15) { |
| 174 | F = (B & C) | (~B & D) |
| 175 | g = i |
| 176 | } else if (i <= 31) { |
| 177 | F = (D & B) | (~D & C) |
| 178 | g = (5 * i + 1) % 16 |
| 179 | } else if (i <= 47) { |
| 180 | F = B ^ C ^ D |
| 181 | g = (3 * i + 5) % 16 |
| 182 | } else { |
| 183 | F = C ^ (B | ~D) |
| 184 | g = (7 * i) % 16 |
| 185 | } |
| 186 | |
| 187 | F = (F + A + K[i] + chunk[g]) >>> 0 |
| 188 | A = D |
| 189 | D = C |
| 190 | C = B |
| 191 | B = ((B + rotateLeft(F, S[i])) & 0xffffffff) >>> 0 |
| 192 | } |
| 193 | |
| 194 | // add values for this chunk to main hash variables (unsigned) |
| 195 | a0 = (a0 + A) >>> 0 |
| 196 | b0 = (b0 + B) >>> 0 |
| 197 | c0 = (c0 + C) >>> 0 |
| 198 | d0 = (d0 + D) >>> 0 |
| 199 | }) |
| 200 | |
| 201 | return u32ToU8([a0, b0, c0, d0]) |
| 202 | } |
| 203 | |
| 204 | // export MD5 function |
| 205 | export { MD5 } |