diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..64b4ecd --- /dev/null +++ b/deps.ts @@ -0,0 +1,2 @@ + +export { createHash } from "https://deno.land/std@0.77.0/hash/mod.ts"; diff --git a/lib/entropy/quasi.ts b/lib/entropy/quasi.ts index ff0f005..3719dbe 100644 --- a/lib/entropy/quasi.ts +++ b/lib/entropy/quasi.ts @@ -1,71 +1,67 @@ +import {createHash} from '../../deps.ts'; + /** * Reproducible salts from input without prior knowledge */ export class EnchantrixEntropyQuasi { - /** - * Extend to enforce a custom mapping - * - * @protected - */ - // deno-lint-ignore no-explicit-any - protected charMap: any = { - " ": "", - "o": "0", - "l": "1", - "e": "3", - "a": "4", - "s": "z", - "t": "7", - }; - /** - * @type {string} Origin Input - * @protected - */ - protected readonly _input: string = ""; - /** - * Initiate with input to work on - * - * @param input - */ - constructor(input: string) { - this._input = input; - } + constructor() { + } - /** - * Returns CharMap - * - * @returns {{'[char]': string}} - */ - // deno-lint-ignore no-explicit-any - get keyMap(): any { - return this.charMap; - } + /** + * Holds the defult mapping swaps + * @private + */ + protected static _keyMap = { + 'o': '0', + 'l': '1', + 'e': '3', + 'a': '4', + 's': 'z', + 't': '7', + '0': 'o', + '1': 'l', + '3': 'e', + '4': 'a', + '7': 't' + }; - /** - * Performs salt on input - * - * @returns {string} Salted Input - */ - salty(): string { - if (!this._input) { - return ""; - } + static get keyMap(): any { + return this._keyMap; + } - let i: number = this._input.length; + static set keyMap(val) { + this._keyMap = val; + } - const salt: string[] = []; + static changeKeyMap(map: any) { + return this.keyMap(map); + } - while (i--) { - // If Char is in the map, use the replaced value; otherwise use original char - salt.push( - this.keyMap[this._input[i]] !== undefined - ? this.keyMap[this._input[i]] - : this._input[i], - ); - } + static hash(input: string): string { + return createHash('sha256') + .update(input + this.createSalt(input)) + .toString() as string; + } + + /** + * Creates a quasi-salt from a string. + * @param input The input string. + */ + static createSalt(input: string): string { + if (!input) { + return ''; + } + + let i: number = input.length; + let salt: string[] = []; + while (i--) { + const char: string = input[i]; + salt.push(this.keyMap[char] !== undefined ? this.keyMap[char] : char); + } + + return salt.join(''); + } - return salt.join(""); - } } diff --git a/lib/parse/file.test.ts b/lib/parse/file.test.ts index fe583ac..ad29c3d 100644 --- a/lib/parse/file.test.ts +++ b/lib/parse/file.test.ts @@ -2,6 +2,9 @@ import { assertEquals } from "https://deno.land/std@0.122.0/testing/asserts.ts"; import { EnchantrixParseFile } from "./file.ts"; // Compact form: name and function +// +// where "snider" is the mapping and "r3dinS" is the binary return. +// Deno.test("IN: Snider OUT: r3dinS", () => { const x = new EnchantrixParseFile(".dataset/Dont-Panic.webp").load(); assertEquals(x, "r3dinS");