pseudo-crypto
Version:
Hash numeric identifiers.
580 lines (567 loc) • 15.8 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// source/data/golden/3.ts
var primes3, inverses3;
var init__ = __esm({
"source/data/golden/3.ts"() {
"use strict";
primes3 = [
BigInt("2"),
BigInt("2"),
BigInt("7"),
BigInt("17"),
BigInt("53"),
BigInt("151"),
BigInt("457"),
BigInt("1361"),
BigInt("4057"),
BigInt("12197"),
BigInt("36497"),
BigInt("109507"),
BigInt("328481"),
BigInt("985351"),
BigInt("2956061"),
BigInt("8868137"),
BigInt("26604343")
];
inverses3 = [
BigInt("0"),
BigInt("2"),
BigInt("4"),
BigInt("8"),
BigInt("26"),
BigInt("103"),
BigInt("67"),
BigInt("977"),
BigInt("5809"),
BigInt("9689"),
BigInt("1427"),
BigInt("40759"),
BigInt("528092"),
BigInt("492370"),
BigInt("4258625"),
BigInt("3341603"),
BigInt("42057253")
];
}
});
// source/data/golden/10.ts
var primes10, inverses10;
var init__2 = __esm({
"source/data/golden/10.ts"() {
"use strict";
primes10 = [
BigInt("2"),
BigInt("7"),
BigInt("67"),
BigInt("619"),
BigInt("6197"),
BigInt("61813"),
BigInt("618041"),
BigInt("6180341"),
BigInt("61803419"),
BigInt("618034003"),
BigInt("6180339923"),
BigInt("61803398903"),
BigInt("618033988751"),
BigInt("6180339887543"),
BigInt("61803398875019"),
BigInt("618033988749911"),
BigInt("6180339887498971")
];
inverses10 = [
BigInt("0"),
BigInt("3"),
BigInt("3"),
BigInt("979"),
BigInt("1533"),
BigInt("30877"),
BigInt("519561"),
BigInt("9991261"),
BigInt("32652179"),
BigInt("270440667"),
BigInt("229552987"),
BigInt("71918074567"),
BigInt("37014448751"),
BigInt("8888268390407"),
BigInt("46560923756579"),
BigInt("34164613497191"),
BigInt("5187581788398931")
];
}
});
// source/data/golden/13.ts
var primes13, inverses13;
var init__3 = __esm({
"source/data/golden/13.ts"() {
"use strict";
primes13 = [
BigInt("2"),
BigInt("11"),
BigInt("107"),
BigInt("1361"),
BigInt("17657"),
BigInt("229487"),
BigInt("2983139"),
BigInt("38780717"),
BigInt("504149341"),
BigInt("6553941047"),
BigInt("85201233643"),
BigInt("1107616036837"),
BigInt("14399008478509"),
BigInt("187187110220261"),
BigInt("2433432432863387"),
BigInt("31634621627224111"),
BigInt("411250081153910567")
];
inverses13 = [
BigInt("0"),
BigInt("6"),
BigInt("139"),
BigInt("770"),
BigInt("17793"),
BigInt("4946"),
BigInt("3230873"),
BigInt("53485004"),
BigInt("479455745"),
BigInt("6116601228"),
BigInt("56042848808"),
BigInt("1234269361955"),
BigInt("3230380485977"),
BigInt("26179300841010"),
BigInt("3896972692066313"),
BigInt("41607168365184312"),
BigInt("222703042258456608")
];
}
});
// source/data/golden/21.ts
var primes21, inverses21;
var init__4 = __esm({
"source/data/golden/21.ts"() {
"use strict";
primes21 = [
BigInt("2"),
BigInt("13"),
BigInt("277"),
BigInt("5737"),
BigInt("120199"),
BigInt("2524117"),
BigInt("53006381"),
BigInt("1113133949"),
BigInt("23375812639"),
BigInt("490892065387"),
BigInt("10308733372867"),
BigInt("216483400829453"),
BigInt("4546151417418511"),
BigInt("95469179765788463"),
BigInt("2004852775081557641"),
BigInt("42101908276712708867"),
BigInt("884140073810966885477")
];
inverses21 = [
BigInt("0"),
BigInt("13"),
BigInt("121"),
BigInt("2536"),
BigInt("139822"),
BigInt("273148"),
BigInt("68964470"),
BigInt("232358912"),
BigInt("14539997257"),
BigInt("124232667718"),
BigInt("15567082457002"),
BigInt("202207040422328"),
BigInt("3264966975443848"),
BigInt("80030427195322712"),
BigInt("1564052260528651016"),
BigInt("62175833738119301519"),
BigInt("946681698849852772649")
];
}
});
// source/data/golden/26.ts
var primes26, inverses26;
var init__5 = __esm({
"source/data/golden/26.ts"() {
"use strict";
primes26 = [
BigInt("2"),
BigInt("17"),
BigInt("419"),
BigInt("10867"),
BigInt("282427"),
BigInt("7343107"),
BigInt("190920451"),
BigInt("4963931759"),
BigInt("129062223683"),
BigInt("3355617815651"),
BigInt("87246063206927"),
BigInt("2268397643379331"),
BigInt("58978338727860347"),
BigInt("1533436806924367339"),
BigInt("39869356980033548923"),
BigInt("1036603281480872271857"),
BigInt("26951685318502679066009")
];
inverses26 = [
BigInt("0"),
BigInt("23"),
BigInt("555"),
BigInt("4003"),
BigInt("200259"),
BigInt("6405355"),
BigInt("166406827"),
BigInt("2348544655"),
BigInt("99013302891"),
BigInt("1020941221707"),
BigInt("95795217660143"),
BigInt("479830684898347"),
BigInt("10033507223729843"),
BigInt("2312125289585707715"),
BigInt("58012575907549666483"),
BigInt("1204672436721283035409"),
BigInt("26042798102750233448105")
];
}
});
// source/data/golden/52.ts
var primes52, inverses52;
var init__6 = __esm({
"source/data/golden/52.ts"() {
"use strict";
primes52 = [
BigInt("2"),
BigInt("37"),
BigInt("1693"),
BigInt("86923"),
BigInt("4518859"),
BigInt("234979021"),
BigInt("12218908787"),
BigInt("635383255037"),
BigInt("33039929261789"),
BigInt("1718076321612767"),
BigInt("89339968723859369"),
BigInt("4645678373640686797"),
BigInt("241575275429315704687"),
BigInt("12561914322324416642513"),
BigInt("653219544760869665410597"),
BigInt("33967416327565222601344681"),
BigInt("1766305649033391575269916279")
];
inverses52 = [
BigInt("0"),
BigInt("45"),
BigInt("789"),
BigInt("98275"),
BigInt("4792803"),
BigInt("18504197"),
BigInt("3605210299"),
BigInt("872383628629"),
BigInt("6381573343605"),
BigInt("1606093097711647"),
BigInt("122865877613730969"),
BigInt("2043452977264427013"),
BigInt("348656741370725648271"),
BigInt("4661925816478442011441"),
BigInt("21331051151155421175213"),
BigInt("22865657849047106158804377"),
BigInt("2834643315706544678749403975")
];
}
});
// source/data/golden/62.ts
var primes62, inverses62;
var init__7 = __esm({
"source/data/golden/62.ts"() {
"use strict";
primes62 = [
BigInt("2"),
BigInt("41"),
BigInt("2377"),
BigInt("147299"),
BigInt("9132313"),
BigInt("566201239"),
BigInt("35104476161"),
BigInt("2176477521929"),
BigInt("134941606358731"),
BigInt("8366379594239857"),
BigInt("518715534842867629"),
BigInt("32160363160257792067"),
BigInt("1993942515935983106231"),
BigInt("123624435988030952586047"),
BigInt("7664715031257919060334479"),
BigInt("475212331937990981740733777"),
BigInt("29463164580155440867925492921")
];
inverses62 = [
BigInt("0"),
BigInt("59"),
BigInt("1677"),
BigInt("187507"),
BigInt("5952585"),
BigInt("643566407"),
BigInt("22071637057"),
BigInt("294289236153"),
BigInt("88879354792675"),
BigInt("7275288500431249"),
BigInt("530317909162771493"),
BigInt("26341070983562945643"),
BigInt("690999432708367660295"),
BigInt("700366256160323916991"),
BigInt("2343937801448434466383727"),
BigInt("622277944959361296777626545"),
BigInt("1228781989677521424978592137")
];
}
});
// source/data/golden/index.ts
var goldenData;
var init_golden = __esm({
"source/data/golden/index.ts"() {
"use strict";
init__();
init__2();
init__3();
init__4();
init__5();
init__6();
init__7();
goldenData = {
3: { primes: primes3, inverses: inverses3 },
10: { primes: primes10, inverses: inverses10 },
13: { primes: primes13, inverses: inverses13 },
21: { primes: primes21, inverses: inverses21 },
26: { primes: primes26, inverses: inverses26 },
52: { primes: primes52, inverses: inverses52 },
62: { primes: primes62, inverses: inverses62 }
};
}
});
// source/sequencers.ts
function range(from, to, step = 1) {
return Array.from(
{ length: (to - from) / step + 1 },
(_, i) => from + i * step
);
}
function unchain(line) {
return line.split("").map((letter) => letter.charCodeAt(0));
}
var init_sequencers = __esm({
"source/sequencers.ts"() {
"use strict";
}
});
// source/data/index.ts
function derive(type) {
for (const type2 in baseData) {
const characterCount = Number(baseData[type2].characterCount);
characterSetData[type2] = {
...baseData[type2],
goldenPrimes: goldenData[characterCount].primes,
goldenInverses: goldenData[characterCount].inverses
};
}
return characterSetData[type];
}
var characterSetData, baseData;
var init_data = __esm({
"source/data/index.ts"() {
"use strict";
init_golden();
init_sequencers();
characterSetData = {};
baseData = {
numeric: {
fillString: "0",
characterCount: BigInt(10),
characterCodes: range(48, 57)
},
inline: {
fillString: "a",
characterCount: BigInt(13),
characterCodes: unchain("acemnorsuvwxz")
},
wide: {
fillString: "a",
characterCount: BigInt(21),
characterCodes: unchain("abcdeghkmnopqrsuvwxyz")
},
thin: {
fillString: "I",
characterCount: BigInt(3),
characterCodes: unchain("Iil")
},
lowercase: {
fillString: "a",
characterCount: BigInt(26),
characterCodes: range(97, 122)
},
uppercase: {
fillString: "A",
characterCount: BigInt(26),
characterCodes: range(65, 90)
},
alphabetic: {
fillString: "A",
characterCount: BigInt(52),
characterCodes: [...range(65, 90), ...range(97, 122)]
},
alphanumeric: {
fillString: "0",
characterCount: BigInt(62),
characterCodes: [...range(48, 57), ...range(65, 90), ...range(97, 122)]
}
};
}
});
// source/pseudo-crypto.ts
var require_pseudo_crypto = __commonJS({
"source/pseudo-crypto.ts"(exports, module) {
"use strict";
init_data();
var PseudoCrypto = class {
/**
* @type {boolean}
* Should always unhash to BigInt?
*/
onlyBigInt;
/**
* @type {string}
* Fill string.
*/
fillString;
/**
* @type {bigint}
* Character count.
*/
characterCount;
/**
* @type {array}
* Character codes.
*/
characterCodes;
/**
* @type {array}
* Next primes greater than
* character_count ^ i /
* 1.618033988749894.
*/
goldenPrimes;
/**
* @type {array}
* Modular multiplicative
* inverses of the primes
* (mod character_count ^ i).
*/
goldenInverses;
constructor(type = "alphanumeric", onlyBigInt = false) {
const data = derive(this.adjustType(type));
this.onlyBigInt = onlyBigInt;
this.fillString = data.fillString;
this.characterCount = data.characterCount;
this.characterCodes = data.characterCodes;
this.goldenPrimes = data.goldenPrimes;
this.goldenInverses = data.goldenInverses;
}
adjustType(type) {
switch (type) {
case "09":
case "numeric":
return "numeric";
case "._":
case "inline":
return "inline";
case ".-":
case "wide":
return "wide";
case "-|":
case "thin":
return "thin";
case "az":
case "lowercase":
return "lowercase";
case "AZ":
case "uppercase":
return "uppercase";
case "Az":
case "alphabetic":
return "alphabetic";
case "A9":
case "alphanumeric":
return "alphanumeric";
}
}
base(int) {
let key = "";
const nil = BigInt(0);
while (int > nil) {
const mod = int % this.characterCount;
const asc = this.characterCodes[Number(mod)];
key += String.fromCharCode(asc);
int /= this.characterCount;
}
return key.split("").reverse().join("");
}
hash(number, length = 5) {
if (!length || length >= this.goldenPrimes.length)
length = this.goldenPrimes.length - 1;
const ceil = this.characterCount ** BigInt(length);
const prime = this.goldenPrimes[length];
const dec = BigInt(number) * prime % ceil;
const hash = this.base(dec);
return hash.padStart(length, this.fillString);
}
unbase(key) {
return key.split("").reverse().reduce((accumulator, value, index) => {
const dec = BigInt(this.characterCodes.indexOf(value.charCodeAt(0)));
return accumulator + dec * this.characterCount ** BigInt(index);
}, BigInt(0));
}
unhash(string) {
const length = string.length;
const ceil = this.characterCount ** BigInt(length);
const inverse = this.goldenInverses[length];
const number = this.unbase(string);
const dec = number * inverse % ceil;
return this.onlyBigInt || dec > Number.MAX_SAFE_INTEGER ? dec : Number(dec);
}
};
module.exports = PseudoCrypto;
}
});
// source/main.ts
var require_main = __commonJS({
"source/main.ts"(exports, module) {
var import_pseudo_crypto = __toESM(require_pseudo_crypto());
module.exports = import_pseudo_crypto.default;
}
});
export default require_main();