inthash
Version:
Efficient integer hashing library using Knuth's multiplicative method for Javascript and Typescript, perfect for obfuscating sequential numbers.
45 lines (42 loc) • 1.56 kB
JavaScript
// deno-lint-ignore-file no-explicit-any
import * as dntShim from "./_dnt.shims.js";
import { Hasher } from "./hasher.js";
const isDeno = typeof dntShim.dntGlobalThis.Deno !== "undefined";
const cmd = isDeno ? "deno run jsr:@denostack/inthash/cli" : "npx inthash";
const rawArgs = isDeno ? dntShim.dntGlobalThis.Deno.args : dntShim.dntGlobalThis.process.argv.slice(2);
const cmdSuffix = rawArgs.join(" ");
const args = parse(rawArgs);
const bit = args.b ?? args.bit ?? args.bits ?? 53;
const options = Hasher.generate(bit);
const hasher = new Hasher(options);
console.log(JSON.stringify(options, null, " "));
console.error(`
$ ${cmd}${cmdSuffix ? " " + cmdSuffix : ""} | pbcopy
Now go ahead and paste it into your code! Good luck. :-)
Note: The supported range of integers is from min: 0 to max: ${hasher._max}.
Please make sure your inputs fall within this range.`);
function parse(args) {
const argv = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
let match;
if ((match = arg.match(/^--(b|bit|bits)=(\d+)/))) {
const [, key, value] = match;
argv[key] = +value;
}
else if ((match = arg.match(/^--(b|bit|bits)$/))) {
const [, key] = match;
const next = args[i + 1];
if (next &&
/\d+/.test(next)) {
argv[key] = +next;
i++;
}
}
else if ((match = arg.match(/^-b(\d+)/))) {
argv.b = +match[1];
}
}
return argv;
}