Last active
June 4, 2024 07:19
-
-
Save wmakeev/578a4f2e8e4138b57599dd31caf0aead to your computer and use it in GitHub Desktop.
[hash] #hash #sha256 #md5 #farmhash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import farmhash from "farmhash"; | |
/** | |
* Returns a farmhash | |
* | |
* @param {string} content | |
*/ | |
export function getFarmhash(content) { | |
return farmhash.hash64(stable); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createHash } from "node:crypto"; | |
/** | |
* Returns a MD5 hash | |
* | |
* @param {import('node:crypto').BinaryLike} content | |
*/ | |
export function md5(content) { | |
return createHash("md5").update(content).digest("hex"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import assert from "assert"; | |
import stringify from "safe-stable-stringify"; | |
/** | |
* Returns JSON object hash. | |
* | |
* @param {object} obj | |
* @param {(content: string) => string} hasher | |
*/ | |
export function getObjectHash(obj, hasher = (content) => content) { | |
const stable = stringify(obj); | |
assert.ok(stable); | |
return hasher(stable); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a SHA256 hash using SHA-2 for the given `content`. | |
* | |
* @param {import('node:crypto').BinaryLike} content | |
* @see https://en.wikipedia.org/wiki/SHA-2 | |
*/ | |
export function sha256(content) { | |
return createHash("sha256").update(content).digest("hex"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment