UNPKG

@inventivetalent/optimus-ts

Version:

Knuths multiplicative hashing based id obfuscation for javascript (port of https://github.com/jenssegers/optimus).

22 lines (21 loc) 745 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Long = require("long"); const MAX_INT32 = 2147483647; const MULTIPLIER = 4294967296; // 2^32; class Optimus { constructor(prime, inverse, random) { this.prime = Long.fromInt(prime); this.inverse = Long.fromInt(inverse); this.random = Long.fromInt(random); } encode(num) { const n = Long.fromInt(num); return n.mul(this.prime).and(Long.fromInt(MAX_INT32)).xor(this.random).toSigned().toInt(); } decode(num) { const n = Long.fromInt(num); return n.xor(this.random).mul(this.inverse).and(Long.fromInt(MAX_INT32)).toSigned().toInt(); } } exports.Optimus = Optimus;