Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active June 4, 2024 07:19
Show Gist options
  • Save wmakeev/578a4f2e8e4138b57599dd31caf0aead to your computer and use it in GitHub Desktop.
Save wmakeev/578a4f2e8e4138b57599dd31caf0aead to your computer and use it in GitHub Desktop.
[hash] #hash #sha256 #md5 #farmhash
import farmhash from "farmhash";
/**
* Returns a farmhash
*
* @param {string} content
*/
export function getFarmhash(content) {
return farmhash.hash64(stable);
}
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");
}
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);
}
/**
* 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