Git Product home page Git Product logo

inthash's Introduction

inthash

Build Coverage License Language Typescript
JSR version Deno version NPM Version Downloads

inthash is a versatile library for generating integer hash values in Javascript and Typescript using Knuth's multiplicative method. With a user-friendly interface, this library allows you to obfuscate predictable numbers, making it ideal for scenarios like 'Auto Increment' values in databases. inthash supports number, string, bigint.

Installation

Node.js

npm install inthash

Deno

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

Usage

Generating Random Settings

Run the following command to generate random settings for your hasher:

# Node.js:
npx inthash

# Deno:
deno run jsr:@denostack/inthash/cli

# Bun
bunx inthash

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

Creating and Using a Hasher

Create a hasher with the generated settings:

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

diagram

// You can obfuscate predictable numbers like 'Auto Increment'!
hasher.encode(0); // 969402349590075
hasher.encode(1); // 6085136369434450
hasher.encode(2); // 4132187376469225
hasher.encode(3); // 2180123214014976

hasher.encode(Number.MAX_SAFE_INTEGER - 3); // 2024647471942759
hasher.encode(Number.MAX_SAFE_INTEGER - 2); // 6827076040726014
hasher.encode(Number.MAX_SAFE_INTEGER - 1); // 4875011878271765
hasher.encode(Number.MAX_SAFE_INTEGER); // 2922062885306540

inthash also supports string and bigint values:

// String input and output
const encoded = hasher.encode("100"); // "6432533451586367"
const decoded = hasher.decode(encoded); // "100"
// BigInt input and output
const encoded = hasher.encode(100n); // 6432533451586367n
const decoded = hasher.decode(encoded); // 100n

Handling MySQL bigint(20)

To work with bigint(20) in MySQL, you need to handle 64-bit values. The old version of IntHash supported up to 53-bit values (Number.MAX_SAFE_INTEGER === 2**53 - 1). From v3 onwards, n-bit values are 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"
# }

See also

  • optimus A PHP implementation of Knuth's multiplicative hashing method. inthash is inspired by and ported from this library.

inthash's People

Contributors

renovate-bot avatar renovate[bot] avatar wan2land avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

inthash's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Information on collisions

Hi, thank you for this awesome library.
I wonder if this library can generate collisions. From what I've seen searching around the web, it shouldn't happen, right? Thanks!

P.S. I'm working with 63 bit integers (0-9223372036854775807).

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Repository problems

These problems occurred while renovating this repository.

  • WARN: App has not been granted permissions to update Workflows - aborting branch.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • Update actions/checkout action to v3
  • Update actions/setup-node action to v3

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.


  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.