MCPcopy Create free account
hub / github.com/node-modules/utility / hash

Function hash

src/crypto.ts:18–36  ·  view source on GitHub ↗
(method: string, s: HashInput, format?: BinaryToTextEncoding)

Source from the content-addressed store, hash-verified

16 * @public
17 */
18export 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

Callers 4

md5Function · 0.85
sha1Function · 0.85
sha256Function · 0.85
sha512Function · 0.85

Calls 1

sortObjectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…