UNPKG

pseudo-shuffle

Version:

Make the index look like it is shuffled according to the range so that it is not conflicted without the actual shuffle.

1 lines 4.7 kB
{"version":3,"file":"pseudo-shuffle.cjs","sources":["../src/pseudo-shuffle.ts"],"sourcesContent":["import fe1 from \"node-fe1-fpe\";\r\n\r\nexport interface IPsuedoShuffleOption {\r\n /**\r\n * The starting value of the shuffleable range.\r\n */\r\n min: number;\r\n /**\r\n * The ending value of the shuffleable range.\r\n *\r\n * Algorithm can be applied only when the difference\r\n * between the min and max values is at least 4.\r\n */\r\n max: number;\r\n /**\r\n * The index value to be shuffled.\r\n */\r\n index: number;\r\n /**\r\n * The private key used to encrypt the index value.\r\n *\r\n * If not specified, the default value is used.\r\n * (**Default value:** `psuedo-shuffle`)\r\n */\r\n privateKey?: string;\r\n /**\r\n * The public key used to encrypt the index value.\r\n *\r\n * If not specified, the default value is used.\r\n * (**Default value:** `psuedo-shuffle`)\r\n */\r\n publicKey?: string;\r\n}\r\n\r\nconst defaultKey = \"psuedo-shuffle\";\r\n\r\nexport const encode = ({\r\n index,\r\n min,\r\n max,\r\n privateKey,\r\n publicKey,\r\n}: IPsuedoShuffleOption): number => {\r\n // Algorithm can be applied only when the difference\r\n // between the min and max values is at least 4.\r\n if (max - min < 3) return index;\r\n\r\n // Algorithms can only be applied when the\r\n // range difference between min and max is prime number.\r\n // Therefore, when the range difference is not prime number,\r\n // the algorithm is applied while leaving the last index intact.\r\n // if ((max - min) % 2 === 0) {\r\n // if (index === max) return index\r\n // --max\r\n // }\r\n if ((max - min) % 2 === 0) {\r\n const middle = Math.ceil(min + (max - min) / 2);\r\n if (index === middle) return max;\r\n if (index === max) index = middle;\r\n --max;\r\n }\r\n\r\n // Algorithm does not apply to index\r\n // values that are not in the range.\r\n if (index < min || index > max) return index;\r\n\r\n return (\r\n fe1.encrypt(\r\n max - min + 1,\r\n index - min,\r\n privateKey ?? defaultKey,\r\n publicKey ?? defaultKey\r\n ) + min\r\n );\r\n};\r\n\r\nexport const decode = ({\r\n index,\r\n min,\r\n max,\r\n privateKey,\r\n publicKey,\r\n}: IPsuedoShuffleOption): number => {\r\n // Algorithm can be applied only when the difference\r\n // between the min and max values is at least 4.\r\n if (max - min < 3) return index;\r\n\r\n // Algorithms can only be applied when the\r\n // range difference between min and max is prime number.\r\n // Therefore, when the range difference is not prime number,\r\n // the algorithm is applied while leaving the last index intact.\r\n\r\n const isNonPrime = (max - min) % 2 === 0;\r\n if (isNonPrime) {\r\n if (index > max - 1) {\r\n return Math.ceil(min + (max - min) / 2);\r\n }\r\n --max;\r\n }\r\n\r\n // Algorithm does not apply to index\r\n // values that are not in the range.\r\n if (index < min || index > max) return index;\r\n\r\n if (isNonPrime) {\r\n if (\r\n index ===\r\n encode({\r\n index: Math.ceil(min + (max - min) / 2),\r\n max,\r\n min,\r\n privateKey: privateKey ?? defaultKey,\r\n publicKey: publicKey ?? defaultKey,\r\n })\r\n )\r\n return max + 1;\r\n }\r\n return (\r\n fe1.decrypt(\r\n max - min + 1,\r\n index - min,\r\n privateKey ?? defaultKey,\r\n publicKey ?? defaultKey\r\n ) + min\r\n );\r\n};\r\n"],"names":["defaultKey","encode","_ref","index","min","max","privateKey","publicKey","middle","Math","ceil","fe1","encrypt","_ref2","isNonPrime","decrypt"],"mappings":"sHAkCMA,EAAa,iBAENC,EAAS,SAAHC,OACjBC,EAAKD,EAALC,MACAC,EAAGF,EAAHE,IACAC,EAAGH,EAAHG,IACAC,EAAUJ,EAAVI,WACAC,EAASL,EAATK,UAIA,GAAIF,EAAMD,EAAM,EAAG,OAAOD,EAU1B,IAAKE,EAAMD,GAAO,GAAM,EAAG,CACzB,IAAMI,EAASC,KAAKC,KAAKN,GAAOC,EAAMD,GAAO,GAC7C,GAAID,IAAUK,EAAQ,OAAOH,EACzBF,IAAUE,IAAKF,EAAQK,KACzBH,CACH,CAID,OAAIF,EAAQC,GAAOD,EAAQE,EAAYF,EAGrCQ,EAAG,QAACC,QACFP,EAAMD,EAAM,EACZD,EAAQC,EACRE,MAAAA,EAAAA,EAAcN,EACdO,MAAAA,EAAAA,EAAaP,GACXI,CAER,iBAEsB,SAAHS,GACjB,IAAAV,EAAKU,EAALV,MACAC,EAAGS,EAAHT,IACAC,EAAGQ,EAAHR,IACAC,EAAUO,EAAVP,WACAC,EAASM,EAATN,UAIA,GAAIF,EAAMD,EAAM,EAAG,OAAOD,EAO1B,IAAMW,GAAcT,EAAMD,GAAO,GAAM,EACvC,GAAIU,EAAY,CACd,GAAIX,EAAQE,EAAM,EAChB,OAAOI,KAAKC,KAAKN,GAAOC,EAAMD,GAAO,KAErCC,CACH,CAID,OAAIF,EAAQC,GAAOD,EAAQE,EAAYF,EAEnCW,GAEAX,IACAF,EAAO,CACLE,MAAOM,KAAKC,KAAKN,GAAOC,EAAMD,GAAO,GACrCC,IAAAA,EACAD,IAAAA,EACAE,WAAsB,MAAVA,EAAAA,EAAcN,EAC1BO,UAAoB,MAATA,EAAAA,EAAaP,IAGnBK,EAAM,EAGfM,EAAG,QAACI,QACFV,EAAMD,EAAM,EACZD,EAAQC,QACRE,EAAAA,EAAcN,QACdO,EAAAA,EAAaP,GACXI,CAER"}