Skip to content
/ lthash Public

LtHash implementation in pure JavaScript.

License

Notifications You must be signed in to change notification settings

yuhr/lthash

Repository files navigation



LtHash

GitHub

LtHash implementation in pure JavaScript.



lthash provides a minimal API to construct Facebook's LtHash (see Section 2.3 of the whitepaper). This package exports a single class LtHash that extends Uint8Array.

It's bundled with dnt, so it should work in browsers, Deno and Node.js.

Installation

For Deno, pick your favorite registry from the following:

  • import LtHash from "https://nest.land/package/lthash/src/index.ts"
  • import LtHash from "https://deno.land/x/lthash/index.ts"
  • import LtHash from "https://esm.sh/lthash"

For Node.js, just install lthash from the npm registry:

  • npm install lthash
  • pnpm add lthash
  • yarn add lthash

Usage

import { Shake256 } from "https://deno.land/std/hash/sha3.ts"
import { encode } from "https://deno.land/std/encoding/hex.ts"
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"
import LtHash from "https://deno.land/x/lthash/LtHash.ts"

Deno.test("LtHash", () => {
	const components = 12
	const hasher = (entry: string) => {
		const shake = new Shake256(components * 8)
		shake.update(entry)
		return new Uint8Array(shake.digest())
	}
	const textDecoder = new TextDecoder()
	const hex = (bytes: Uint8Array) => textDecoder.decode(encode(bytes))

	const lthash = new LtHash(components, hasher)
	assertEquals("000000000000000000000000", hex(lthash))
	assertEquals("1234075ae4a1e77316cf2d80", hex(lthash.add("hello")))
	assertEquals("81325c99041731d8b089173c", hex(lthash.add("world")))
	assertEquals("1234075ae4a1e77316cf2d80", hex(lthash.remove("world")))
	assertEquals("000000000000000000000000", hex(lthash.remove("hello")))
	assertEquals("6ffe553f20764a659abaeabc", hex(lthash.add("world")))
	assertEquals("81325c99041731d8b089173c", hex(lthash.add("hello")))
	assertEquals("1234075ae4a1e77316cf2d80", hex(lthash.remove("world")))
	assertEquals("000000000000000000000000", hex(lthash.remove("hello")))
})