Skip to main content

inthash

Build Coverage Downloads Version deno.land/x/inthash License Language Typescript

Integer Hashing Library based on Knuth’s multiplicative hashing method for Javascript(& Typescript).

Installation

Node.js

npm install inthash

Deno

import { Hasher } from "https://deno.land/x/inthash/mod.ts";

Usage

Generate random settings with the following command:

# Node.js:
npx inthash

# Deno:
deno run https://deno.land/x/inthash/cli.ts

# Output:
# {
#   "bits": 53,
#   "prime": "6456111708547433",
#   "inverse": "3688000043513561",
#   "xor": "969402349590075"
# }

And create hasher like this:

const hasher = new Hasher({
  bits: 53, // Javascript, Number.MAX_SAFE_INTEGER
  prime: "6456111708547433", // Random Prime
  inverse: "3688000043513561", // Modular Inverse
  xor: "969402349590075", // Random n-bit xor mask
});

const encoded = hasher.encode(100); // result: 6432533451586367
const decoded = hasher.decode(encoded); // result: 100

// You can obfuscate predictable numbers like 'Auto Increment'!
hasher.encode(1); // 6085136369434450
hasher.encode(2); // 4132187376469225
hasher.encode(3); // 2180123214014976
hasher.encode(4); // 6982551782798239
hasher.encode(5); // 5030633649101110
hasher.encode(6); // 3077950944243277
hasher.encode(7); // 1125015438342116

string and bigint values are also available.

// string in-out
const encoded = hasher.encode("100"); // "6432533451586367"
const decoded = hasher.decode(encoded); // "100"
// bigint in-out
const encoded = hasher.encode(100n); // 6432533451586367n
const decoded = hasher.decode(encoded); // 100n

How can I use MySQL bigint(20)?

To handle bigint(20) in mysql, you have to deal with 64bit. Old version inthash library only supported up to 53bit(Number.MAX_SAFE_INTEGER === 2**53 - 1) From v3 or later, n-bit is supported. :-)

# Node.js:
npx inthash -b64

# Deno:
deno run https://deno.land/x/inthash/cli.ts -b64

# Output:
# {
#   "bits": 64,
#   "prime": "16131139598801670337",
#   "inverse": "14287487925114175297",
#   "xor": "8502035541264656686"
# }

Refs.