(method: string, s: HashInput, format?: BinaryToTextEncoding)
| 16 | * @public |
| 17 | */ |
| 18 | export function hash(method: string, s: HashInput, format?: BinaryToTextEncoding): string { |
| 19 | if (s instanceof ArrayBuffer) { |
| 20 | s = Buffer.from(s); |
| 21 | } |
| 22 | const isBuffer = Buffer.isBuffer(s) || ArrayBuffer.isView(s); |
| 23 | if (!isBuffer && typeof s === 'object') { |
| 24 | s = JSON.stringify(sortObject(s)); |
| 25 | } |
| 26 | |
| 27 | if (nativeHash) { |
| 28 | // try to use crypto.hash first |
| 29 | // https://nodejs.org/en/blog/release/v21.7.0#crypto-implement-cryptohash |
| 30 | return nativeHash(method, s, format); |
| 31 | } |
| 32 | |
| 33 | const sum = createHash(method); |
| 34 | sum.update(s as string, isBuffer ? 'binary' : 'utf8'); |
| 35 | return sum.digest(format || 'hex'); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * md5 hash |
no test coverage detected
searching dependent graphs…