| 244 | * @throws {Error} If `callback` is present but not a function |
| 245 | */ |
| 246 | export function compare(password, hashValue, callback, progressCallback) { |
| 247 | function _async(callback) { |
| 248 | if (typeof password !== "string" || typeof hashValue !== "string") { |
| 249 | nextTick( |
| 250 | callback.bind( |
| 251 | this, |
| 252 | Error( |
| 253 | "Illegal arguments: " + typeof password + ", " + typeof hashValue, |
| 254 | ), |
| 255 | ), |
| 256 | ); |
| 257 | return; |
| 258 | } |
| 259 | if (hashValue.length !== 60) { |
| 260 | nextTick(callback.bind(this, null, false)); |
| 261 | return; |
| 262 | } |
| 263 | hash( |
| 264 | password, |
| 265 | hashValue.substring(0, 29), |
| 266 | function (err, comp) { |
| 267 | if (err) callback(err); |
| 268 | else callback(null, safeStringCompare(comp, hashValue)); |
| 269 | }, |
| 270 | progressCallback, |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | if (callback) { |
| 275 | if (typeof callback !== "function") |
| 276 | throw Error("Illegal callback: " + typeof callback); |
| 277 | _async(callback); |
| 278 | } else |
| 279 | return new Promise(function (resolve, reject) { |
| 280 | _async(function (err, res) { |
| 281 | if (err) { |
| 282 | reject(err); |
| 283 | return; |
| 284 | } |
| 285 | resolve(res); |
| 286 | }); |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Gets the number of rounds used to encrypt the specified hash. |