| 201 | * @returns {string | Buffer} digest |
| 202 | */ |
| 203 | const digest = (hash, encoding, isSafe) => { |
| 204 | if (typeof encoding === "undefined") { |
| 205 | return isSafe ? hash.digest() : toBuffer(hash.digest()); |
| 206 | } |
| 207 | |
| 208 | if (encoding === "base64url" && !isBase64URLSupported) { |
| 209 | const digest = isSafe |
| 210 | ? hash.digest("base64") |
| 211 | : toString(hash.digest("base64"), "base64"); |
| 212 | |
| 213 | return digest.replace(/\+/g, "-").replace(/\//g, "_").replace(/[=]+$/, ""); |
| 214 | } else if ( |
| 215 | typeof ENCODE_TABLE[/** @type {Base} */ (encoding.slice(4))] !== "undefined" |
| 216 | ) { |
| 217 | const buf = isSafe ? hash.digest() : toBuffer(hash.digest()); |
| 218 | |
| 219 | return encode( |
| 220 | buf, |
| 221 | /** @type {Base} */ |
| 222 | (encoding.slice(4)) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | return isSafe |
| 227 | ? hash.digest(encoding) |
| 228 | : toString(hash.digest(encoding), encoding); |
| 229 | }; |
| 230 | |
| 231 | module.exports.decode = decode; |
| 232 | module.exports.digest = digest; |