| 110 | * @throws {Error} If `callback` is present but not a function |
| 111 | */ |
| 112 | export function genSalt(rounds, seed_length, callback) { |
| 113 | if (typeof seed_length === "function") |
| 114 | (callback = seed_length), (seed_length = undefined); // Not supported. |
| 115 | if (typeof rounds === "function") (callback = rounds), (rounds = undefined); |
| 116 | if (typeof rounds === "undefined") rounds = GENSALT_DEFAULT_LOG2_ROUNDS; |
| 117 | else if (typeof rounds !== "number") |
| 118 | throw Error("illegal arguments: " + typeof rounds); |
| 119 | |
| 120 | function _async(callback) { |
| 121 | nextTick(function () { |
| 122 | // Pretty thin, but salting is fast enough |
| 123 | try { |
| 124 | callback(null, genSaltSync(rounds)); |
| 125 | } catch (err) { |
| 126 | callback(err); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | if (callback) { |
| 132 | if (typeof callback !== "function") |
| 133 | throw Error("Illegal callback: " + typeof callback); |
| 134 | _async(callback); |
| 135 | } else |
| 136 | return new Promise(function (resolve, reject) { |
| 137 | _async(function (err, res) { |
| 138 | if (err) { |
| 139 | reject(err); |
| 140 | return; |
| 141 | } |
| 142 | resolve(res); |
| 143 | }); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Synchronously generates a hash for the given password. |