| 169 | * @throws {Error} If `callback` is present but not a function |
| 170 | */ |
| 171 | export function hash(password, salt, callback, progressCallback) { |
| 172 | function _async(callback) { |
| 173 | if (typeof password === "string" && typeof salt === "number") |
| 174 | genSalt(salt, function (err, salt) { |
| 175 | _hash(password, salt, callback, progressCallback); |
| 176 | }); |
| 177 | else if (typeof password === "string" && typeof salt === "string") |
| 178 | _hash(password, salt, callback, progressCallback); |
| 179 | else |
| 180 | nextTick( |
| 181 | callback.bind( |
| 182 | this, |
| 183 | Error("Illegal arguments: " + typeof password + ", " + typeof salt), |
| 184 | ), |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | if (callback) { |
| 189 | if (typeof callback !== "function") |
| 190 | throw Error("Illegal callback: " + typeof callback); |
| 191 | _async(callback); |
| 192 | } else |
| 193 | return new Promise(function (resolve, reject) { |
| 194 | _async(function (err, res) { |
| 195 | if (err) { |
| 196 | reject(err); |
| 197 | return; |
| 198 | } |
| 199 | resolve(res); |
| 200 | }); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Compares two strings of the same length in constant time. |