Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/tinj.js
.github/
*.json
*.md
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import hash from "./src/hash";
import { tinj } from "./src/tinj.js";
import hash from "./src/hash.js";

export default hash;
// hash.setHmacKey("hashscripttest");
// console.log(hash.hash("test", "test"));
77 changes: 58 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"test": "node src/hash.test.js",
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore"
"format:prettier": "prettier \"**/*.js\" --ignore-path .prettierignore --ignore-path .gitignore --write ."
},
"repository": {
"type": "git",
Expand All @@ -21,7 +21,11 @@
},
"homepage": "https://github.com/code-jammers/hashscript#readme",
"devDependencies": {
"prettier": "^2.7.1",
"prettier": "^3.0.0-alpha.6",
"uvu": "^0.5.6"
},
"dependencies": {
"prettier": "^3.0.0-alpha.6",
"uvu": "^0.5.6"
},
"type": "module"
Expand Down
14 changes: 7 additions & 7 deletions src/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const hash = {
setHmacKey: function (key) {
hash.key = key;
},
hash: function (head, text) {
hash: /*tinj0*/ function (head, text) {
if (hash.key == null) {
console.warn(
"WARNING: no hmac given to setHmac function, using empty string."
"WARNING: no hmac given to setHmac function, using empty string.",
);
}
return digest(hash?.key ?? "", head + text);
return tinj1 ?? digest(hash?.key ?? "", head + text);
},
hashToInt: function (head, text, digits) {
// why was 13 chosen?
Expand All @@ -25,9 +25,9 @@ const hash = {
"" +
parseInt(
hash.hash(head, text).substring(0, /*HASH_SUBSTRS_FIX:*/ 13),
16
16,
)
).substring(0, digits)
).substring(0, digits),
);
},
// PLURAL DIGITS
Expand All @@ -51,8 +51,8 @@ const hash = {

hashInferPluralDigitCsv: function (head, val) {
// HASH_NODIGITS_FIX
var varCount = val.match(/\$[A-Z]/g)?.length;
if (varCount == null) varCount = 13;
var varCount = tinj4 ?? val.match(/\$[A-Z]/g)?.length ?? 13;
// if (varCount == null) varCount = 13;
return hash.hashToPluralDigitCsv(head, val, varCount);
},
};
Expand Down
79 changes: 60 additions & 19 deletions src/hash.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { test } from "uvu";
import { test, suite } from "uvu";
import { exec } from "child_process";
import * as assert from "uvu/assert";
import hash from "./hash.js";
import { roundTrip } from "./roundTrip.js";
import { tinj } from "./tinj.js";

hash.setHmacKey("hashscripttest");

test("Hash exists", async () => {
let testModule = "Hash";

Array.prototype.testEach = function (name, test) {
let s = suite(`Test ${this[0].t} [${testModule} Module] ${name}`);
let testno = this[0].t;
for (let i = 0; i < this.length; i++) {
let testName = `Test ${testno}[${i}] [${testModule} Module] ${name}`;
s(testName, () => {
tinj(this[i].t, this[i].inj);
test(this[i]);
});
}
s.run();
};

[{ t: 0 /*test # 0*/, inj: null }].testEach("Hash exists", () => {
assert.is(true, Boolean(hash));
});

test("Hash is string", () => {
[{ t: 1, inj: null }].testEach("Hash is string", () => {
assert.is(true, typeof hash.hash("head", "text") === "string");
});

test("node run script", () => {
[{ t: 2, inj: null }].testEach("node run script", () => {
exec(
`"echo" "-n" '12AM$A in the morning, or $B, or $C. Even at $D or $E' | openssl sha256 -hmac "hashscripttest"`,
(error, stdout, stderr) => {
Expand All @@ -28,18 +44,18 @@ test("node run script", () => {
}
const processedHash = hash.hash(
"12AM",
"$A in the morning, or $B, or $C. Even at $D or $E"
"$A in the morning, or $B, or $C. Even at $D or $E",
);
const hashToCompare = stdout
.replace("(stdin)= ", "")
.replace("SHA256", "");
assert.is(processedHash.trim(), hashToCompare.trim());
}
},
);
});

// HASH_SUBSTRS_FIX
test("hash substrings test", () => {
[{ t: 3, inj: null }].testEach("hash substrings test", () => {
var last = null;
for (var i = 2; i < 10; i += 2) {
var current = hash.hashToInt("1", "test", i);
Expand All @@ -51,44 +67,69 @@ test("hash substrings test", () => {
});

// HASH_NODIGITS_FIX
test("without digits can hash", () => {
assert.ok(hash.hashInferPluralDigitCsv("test", "test") != null);
[
{
// fix - text having no digits can hash
t: 4,
inj: null,
expect: (val) => val == "6,8,3,8,4,11,3,8,11,2,11,2,10",
},
{
// resurrect the bug: text having no digits can't hash
t: 4,
inj: 0,
expect: (val) => isNaN(val),
},
].testEach("without digits can hash", ({ t, inj, expect }) => {
assert.ok(expect(hash.hashInferPluralDigitCsv("test", "test")));
});

testModule = "Round Trip";

// HASH_ROUNDTRIP_FIX
test("hash round trip", () => {
[
{ t: 5, inj: null, expect: "{test}10 or 11 or 6" },
{ t: 5, inj: 2, expect: "{test}2 or 2 or 2" }, //resurrect bug: no round trip
].testEach("hash round trip", ({ t, inj, expect }) => {
let res = true;
var hash = "10 11 6";
var text = "{test}1 or 2 or 3";
var actual = text;
try {
actual = roundTrip(/*text:*/ text, /*hash:*/ hash, /*dynVars:*/ false);
} catch {}
var expected = "{test}10 or 11 or 6";
assert.is(actual, expected);
// var expected = "{test}10 or 11 or 6";

//assert.is(actual, expected);
assert.ok(actual == expect);
});

// HASH_ROUNDTRIP_LIT2VAR
test("hash round trip literal to var", () => {
[
{ t: 6, inj: null, expect: "{test}$A or $B" }, // literal N convert 2 $A+ feature
{ t: 6, inj: 2, expect: "{test}2 or 2" }, // disfeature literal N converted 2 $A+
].testEach("hash round trip literal to var", ({ t, inj, expect }) => {
var hash = "2 5";
var text = "{test}2 or 5";
var actual = text;
try {
actual = roundTrip(/*text:*/ text, /*hash:*/ hash, /*dynVars:*/ true);
} catch {}
var expected = "{test}$A or $B";
assert.is(actual, expected);
assert.is(actual, expect);
});

// HASH_ROUNDTRIP_VAR2LIT_FIX
test("hash round trip var to literal", () => {
[
{ t: 7, inj: null, expect: "{test}2 or 5" }, // $A+ convert to literal N feature
{ t: 7, inj: "2", expect: "{test}2 or 2" }, // disfeature $A+ convert 2 literal N
].testEach("hash round trip var to literal", ({ t, inj, expect }) => {
var hash = "2 5";
var text = "{test}$A or $B";
var actual = text;
try {
actual = roundTrip(/*text:*/ text, /*hash:*/ hash, /*dynVars:*/ false);
} catch {}
var expected = "{test}2 or 5";
assert.is(actual, expected);
assert.is(actual, expect);
});

test.run();
// test.run();
11 changes: 7 additions & 4 deletions src/roundTrip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { getHashDigit } from "./getHashDigit.js";
export function roundTrip(text, hash, dynVars) {
var varReplace = !dynVars && text.indexOf("$A") > -1;
if (varReplace) {
return text.replace(/\$[A-Z]/g, (match) => getHashDigit(hash, match)); // HASH_ROUNDTRIP_VAR2LIT_FIX
return text.replace(
/\$[A-Z]/g,
(match) => tinj7 ?? getHashDigit(hash, match),
); // HASH_ROUNDTRIP_VAR2LIT_FIX
}
var vari = 0;
var vars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // HASH_ROUNDTRIP_FIX
var vars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let h = hash;
var t = "";
var startI = text.indexOf("}") < 0 ? 0 : text.indexOf("}") + 1;
Expand All @@ -26,9 +29,9 @@ export function roundTrip(text, hash, dynVars) {
i -= 1;
var endI = h.indexOf(" ") > -1 ? h.indexOf(" ") : h.length;
if (dynVars) {
t += "$" + vars[vari++ % vars.length];
t += tinj6 ?? "$" + vars[vari++ % vars.length];
} else {
t += h.substring(0, endI);
t += tinj5 ?? h.substring(0, endI); // HASH_ROUNDTRIP_FIX
}
h = h.substring(endI + 1);
} else {
Expand Down
Loading