|
| 1 | +/** @preserve |
| 2 | +(c) 2012 by Cédric Mesnil. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without modification, are permitted |
| 5 | +provided that the following conditions are met: |
| 6 | +
|
| 7 | + - Redistributions of source code must retain the above copyright notice, this list of |
| 8 | + conditions and the following disclaimer. |
| 9 | + - Redistributions in binary form must reproduce the above copyright notice, this list |
| 10 | + of conditions and the following disclaimer in the documentation and/or other materials |
| 11 | + provided with the distribution. |
| 12 | +
|
| 13 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 14 | +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 15 | +AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 16 | +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 17 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 18 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 19 | +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY |
| 20 | +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | +*/ |
| 22 | + |
| 23 | +import { |
| 24 | + WordArray |
| 25 | +} from '../../core/core.js'; |
| 26 | + |
| 27 | +import { Hasher } from '../../core/hasher'; |
| 28 | + |
| 29 | +// Constants table |
| 30 | +const _zl = new WordArray([ |
| 31 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 32 | + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, |
| 33 | + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, |
| 34 | + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, |
| 35 | + 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]); |
| 36 | +const _zr = new WordArray([ |
| 37 | + 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, |
| 38 | + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, |
| 39 | + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, |
| 40 | + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, |
| 41 | + 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]); |
| 42 | +const _sl = new WordArray([ |
| 43 | + 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, |
| 44 | + 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, |
| 45 | + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, |
| 46 | + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, |
| 47 | + 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6]); |
| 48 | +const _sr = new WordArray([ |
| 49 | + 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, |
| 50 | + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, |
| 51 | + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, |
| 52 | + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, |
| 53 | + 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11]); |
| 54 | + |
| 55 | +const _hl = new WordArray([0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]); |
| 56 | +const _hr = new WordArray([0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]); |
| 57 | + |
| 58 | +const f1 = (x, y, z) => (x) ^ (y) ^ (z); |
| 59 | + |
| 60 | +const f2 = (x, y, z) => ((x) & (y)) | ((~x) & (z)); |
| 61 | + |
| 62 | +const f3 = (x, y, z) => ((x) | (~(y))) ^ (z); |
| 63 | + |
| 64 | +const f4 = (x, y, z) => ((x) & (z)) | ((y) & (~(z))); |
| 65 | + |
| 66 | +const f5 = (x, y, z) => (x) ^ ((y) | (~(z))); |
| 67 | + |
| 68 | +const rotl = (x, n) => (x << n) | (x >>> (32 - n)); |
| 69 | + |
| 70 | +/** |
| 71 | + * RIPEMD160 hash algorithm. |
| 72 | + */ |
| 73 | +export class RIPEMD160Algo extends Hasher { |
| 74 | + _doReset() { |
| 75 | + this._hash = new WordArray([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]); |
| 76 | + } |
| 77 | + |
| 78 | + _doProcessBlock(M, offset) { |
| 79 | + const _M = M; |
| 80 | + |
| 81 | + // Swap endian |
| 82 | + for (let i = 0; i < 16; i++) { |
| 83 | + // Shortcuts |
| 84 | + const offset_i = offset + i; |
| 85 | + const M_offset_i = _M[offset_i]; |
| 86 | + |
| 87 | + // Swap |
| 88 | + _M[offset_i] = ( |
| 89 | + (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
| 90 | + | (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00) |
| 91 | + ); |
| 92 | + } |
| 93 | + // Shortcut |
| 94 | + const H = this._hash.words; |
| 95 | + const hl = _hl.words; |
| 96 | + const hr = _hr.words; |
| 97 | + const zl = _zl.words; |
| 98 | + const zr = _zr.words; |
| 99 | + const sl = _sl.words; |
| 100 | + const sr = _sr.words; |
| 101 | + |
| 102 | + // Working variables |
| 103 | + let al = H[0]; |
| 104 | + let bl = H[1]; |
| 105 | + let cl = H[2]; |
| 106 | + let dl = H[3]; |
| 107 | + let el = H[4]; |
| 108 | + let ar = H[0]; |
| 109 | + let br = H[1]; |
| 110 | + let cr = H[2]; |
| 111 | + let dr = H[3]; |
| 112 | + let er = H[4]; |
| 113 | + |
| 114 | + // Computation |
| 115 | + let t; |
| 116 | + for (let i = 0; i < 80; i++) { |
| 117 | + t = (al + _M[offset + zl[i]]) | 0; |
| 118 | + if (i < 16) { |
| 119 | + t += f1(bl, cl, dl) + hl[0]; |
| 120 | + } else if (i < 32) { |
| 121 | + t += f2(bl, cl, dl) + hl[1]; |
| 122 | + } else if (i < 48) { |
| 123 | + t += f3(bl, cl, dl) + hl[2]; |
| 124 | + } else if (i < 64) { |
| 125 | + t += f4(bl, cl, dl) + hl[3]; |
| 126 | + } else { // if (i<80) { |
| 127 | + t += f5(bl, cl, dl) + hl[4]; |
| 128 | + } |
| 129 | + t = t | 0; |
| 130 | + t = rotl(t, sl[i]); |
| 131 | + t = (t + el) | 0; |
| 132 | + al = el; |
| 133 | + el = dl; |
| 134 | + dl = rotl(cl, 10); |
| 135 | + cl = bl; |
| 136 | + bl = t; |
| 137 | + |
| 138 | + t = (ar + _M[offset + zr[i]]) | 0; |
| 139 | + if (i < 16) { |
| 140 | + t += f5(br, cr, dr) + hr[0]; |
| 141 | + } else if (i < 32) { |
| 142 | + t += f4(br, cr, dr) + hr[1]; |
| 143 | + } else if (i < 48) { |
| 144 | + t += f3(br, cr, dr) + hr[2]; |
| 145 | + } else if (i < 64) { |
| 146 | + t += f2(br, cr, dr) + hr[3]; |
| 147 | + } else { // if (i<80) { |
| 148 | + t += f1(br, cr, dr) + hr[4]; |
| 149 | + } |
| 150 | + t |= 0; |
| 151 | + t = rotl(t, sr[i]); |
| 152 | + t = (t + er) | 0; |
| 153 | + ar = er; |
| 154 | + er = dr; |
| 155 | + dr = rotl(cr, 10); |
| 156 | + cr = br; |
| 157 | + br = t; |
| 158 | + } |
| 159 | + // Intermediate hash value |
| 160 | + t = (H[1] + cl + dr) | 0; |
| 161 | + H[1] = (H[2] + dl + er) | 0; |
| 162 | + H[2] = (H[3] + el + ar) | 0; |
| 163 | + H[3] = (H[4] + al + br) | 0; |
| 164 | + H[4] = (H[0] + bl + cr) | 0; |
| 165 | + H[0] = t; |
| 166 | + } |
| 167 | + |
| 168 | + _doFinalize() { |
| 169 | + // Shortcuts |
| 170 | + const data = this._data; |
| 171 | + const dataWords = data.words; |
| 172 | + |
| 173 | + const nBitsTotal = this._nDataBytes * 8; |
| 174 | + const nBitsLeft = data.sigBytes * 8; |
| 175 | + |
| 176 | + // Add padding |
| 177 | + dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - (nBitsLeft % 32)); |
| 178 | + dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = ( |
| 179 | + (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
| 180 | + | (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00) |
| 181 | + ); |
| 182 | + data.sigBytes = (dataWords.length + 1) * 4; |
| 183 | + |
| 184 | + // Hash final blocks |
| 185 | + this._process(); |
| 186 | + |
| 187 | + // Shortcuts |
| 188 | + const hash = this._hash; |
| 189 | + const H = hash.words; |
| 190 | + |
| 191 | + // Swap endian |
| 192 | + for (let i = 0; i < 5; i++) { |
| 193 | + // Shortcut |
| 194 | + const H_i = H[i]; |
| 195 | + |
| 196 | + // Swap |
| 197 | + H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
| 198 | + | (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00); |
| 199 | + } |
| 200 | + |
| 201 | + // Return final computed hash |
| 202 | + return hash; |
| 203 | + } |
| 204 | + |
| 205 | + clone() { |
| 206 | + const clone = super.clone.call(this); |
| 207 | + clone._hash = this._hash.clone(); |
| 208 | + |
| 209 | + return clone; |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Shortcut function to the hasher's object interface. |
| 215 | + * |
| 216 | + * @param {WordArray|string} message The message to hash. |
| 217 | + * |
| 218 | + * @return {WordArray} The hash. |
| 219 | + * |
| 220 | + * @static |
| 221 | + * |
| 222 | + * @example |
| 223 | + * |
| 224 | + * var hash = CryptoJS.RIPEMD160('message'); |
| 225 | + * var hash = CryptoJS.RIPEMD160(wordArray); |
| 226 | + */ |
| 227 | +export const RIPEMD160 = Hasher._createHelper(RIPEMD160Algo); |
| 228 | + |
| 229 | +/** |
| 230 | + * Shortcut function to the HMAC's object interface. |
| 231 | + * |
| 232 | + * @param {WordArray|string} message The message to hash. |
| 233 | + * @param {WordArray|string} key The secret key. |
| 234 | + * |
| 235 | + * @return {WordArray} The HMAC. |
| 236 | + * |
| 237 | + * @static |
| 238 | + * |
| 239 | + * @example |
| 240 | + * |
| 241 | + * var hmac = CryptoJS.HmacRIPEMD160(message, key); |
| 242 | + */ |
| 243 | +export const HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160Algo); |
0 commit comments