Efficient integer hashing library using Knuth's multiplicative method for Javascript and Typescript, perfect for obfuscating sequential numbers.
Repository
Current version released
2 years ago
inthash
Integer Hashing Library based on Knuth’s multiplicative hashing method for Javascript(& Typescript).
Installation
Node.js
npm install inthash
import { Hasher } from "inthash";
//
Deno
import { Hasher } from "https://deno.land/x/inthash/mod.ts";
//
Usage
3 values are required before using inthash.
- Prime
- Inverse
- Xor (Random Number)
Fortunately, we provide the CLI tool also.
npx inthash
# deno run https://deno.land/x/inthash/cli.ts
{
"bits": 53,
"prime": "6456111708547433",
"inverse": "3688000043513561",
"xor": "969402349590075"
}
Copy the output code and paste it into your project.
There are only two methods. encode
and decode
.
const hasher = new Hasher({
bits: 53, // Javascript, Number.MAX_SAFE_INTEGER
prime: "6456111708547433",
inverse: "3688000043513561",
xor: "969402349590075",
});
const encoded = hasher.encode(100); // 6432533451586367
const decoded = hasher.decode(encoded); // 100
Done!
Of course, you can use other types of values as well.
// string
const encoded = hasher.encode("100"); // "6432533451586367"
const decoded = hasher.decode(encoded); // "100"
// bigint
const encoded = hasher.encode(100n); // 6432533451586367n
const decoded = hasher.decode(encoded); // 100n
bigint(20)
with MySQL To handle bigint(20)
in mysql, you have to deal with 64bit. In the past
inthash
library only supported up to 53bit(Number.MAX_SAFE_INTEGER(2^53-1)
),
the maximum value of JavaScript integer type. From v3.0.0 onwards, 64-bit is
also available. :-)
npx inthash -b64
# deno run https://deno.land/x/inthash/cli.ts -b64
{
"bits": 64,
"prime": "16131139598801670337",
"inverse": "14287487925114175297",
"xor": "8502035541264656686"
}