JavaScript Avolitty Hasher
Description
Create variable-length checksums from all data types with a fast and unique hashing algorithm.
- All MDN modern web browsers supported
- All data types and values supported
- Browser JavaScript and Node.js support
- Circular object references supported
- Considers C programming constraints relevant to JavaScript
- Fast hashing speed with collision resistance
- Minified and readable code with single-letter variable names
- No dependencies
- Optimized for JavaScript UTF-16 code units
- Optimized for efficient memory usage
- Option to ignore object key sort order
- Option to set hash digest length between
0
and 2147483647
- Vast difference in hashed output from minor change to input
Usage
Install the NPM module in the current directory with the command npm install
.
npm install avolitty-hasher
Include the module in Node.js with the function require()
.
const avolittyHasher = require("avolitty-hasher");
The following example uses code from the file test.js to generate hash digests.
const a = "🥑";
const b = {
"outputLength": 128
};
const c = avolittyHasher(a, b);
console.log(c);
The function avolittyHasher()
outputs a variable-length hexadecimal hash digest.
The return value variable c
is a string
defined as the hexadecimal checksum digest.
The first argument variable a
is a boolean
, a function
, a number
, an object
, a string
or an undefined
defined as the message payload to hash.
The second argument variable b
is an object
defined as the following options.
{
"ignoreObjectKeySortOrder": true,
"outputLength": 8
}
The optional property ignoreObjectKeySortOrder
is a boolean
defined as true
to ignore the sort order of object keys.
The default value is defined as false
.
When defined as true
, salt is added to avoid collisions from the same object when defined as false
.
The required property outputLength
is a number
defined as a positive whole number.
The command node
can process a list of defined test inputs from the file test/index.js.
node test
The output is a list of test inputs with corresponding outputs from the function avolittyHasher()
.
The values can be compared with the expected output below, excluding the dynamic variable global
.
Input: avolittyHasher(global, {"outputLength": 8});
Output: "9cd7a03e"
Input: avolittyHasher("global", {"outputLength": 8});
Output: "54dd196c"
Input: avolittyHasher(global.clearTimeout, {"outputLength": 8});
Output: "4097ee92"
Input: avolittyHasher("global.clearTimeout", {"outputLength": 8});
Output: "e8816253"
Input: avolittyHasher(function() {}, {"outputLength": 8});
Output: "76045d2c"
Passed: Yes
Input: avolittyHasher("function() {}", {"outputLength": 8});
Output: "92892288"
Passed: Yes
Input: avolittyHasher(function(a) {}, {"outputLength": 8});
Output: "967f8dcc"
Passed: Yes
Input: avolittyHasher("function(a) {}", {"outputLength": 8});
Output: "f109ad2f"
Passed: Yes
Input: avolittyHasher(function(a) {return}, {"outputLength": 8});
Output: "48cf5787"
Passed: Yes
Input: avolittyHasher("function(a) {return}", {"outputLength": 8});
Output: "e28ee00a"
Passed: Yes
Input: avolittyHasher(function(a) { return}, {"outputLength": 8});
Output: "41087df8"
Passed: Yes
Input: avolittyHasher("function(a) { return}", {"outputLength": 8});
Output: "41a229dd"
Passed: Yes
Input: avolittyHasher({}, {"outputLength": 8});
Output: "22c3f299"
Passed: Yes
Input: avolittyHasher("{}", {"outputLength": 8});
Output: "89b780a1"
Passed: Yes
Input: avolittyHasher({"0": 0}, {"outputLength": 8});
Output: "3432b2ac"
Passed: Yes
Input: avolittyHasher({"0": 1}, {"outputLength": 8});
Output: "586bd3f0"
Passed: Yes
Input: avolittyHasher([], {"outputLength": 8});
Output: "4fa52f4d"
Passed: Yes
Input: avolittyHasher("[]", {"outputLength": 8});
Output: "05921abb"
Passed: Yes
Input: avolittyHasher([0], {"outputLength": 8});
Output: "fb3b2316"
Passed: Yes
Input: avolittyHasher("[0]", {"outputLength": 8});
Output: "47639828"
Passed: Yes
Input: avolittyHasher(null, {"outputLength": 8});
Output: "93466dc8"
Passed: Yes
Input: avolittyHasher("null", {"outputLength": 8});
Output: "229f2295"
Passed: Yes
Input: avolittyHasher(true, {"outputLength": 8});
Output: "7e744397"
Passed: Yes
Input: avolittyHasher("true", {"outputLength": 8});
Output: "229f2295"
Passed: Yes
Input: avolittyHasher(false, {"outputLength": 8});
Output: "c356fbac"
Passed: Yes
Input: avolittyHasher("false", {"outputLength": 8});
Output: "0d3f42fd"
Passed: Yes
Input: avolittyHasher(Infinity, {"outputLength": 8});
Output: "a9d97ddf"
Passed: Yes
Input: avolittyHasher("Infinity", {"outputLength": 8});
Output: "10235f96"
Passed: Yes
Input: avolittyHasher(-Infinity, {"outputLength": 8});
Output: "6fcf567d"
Passed: Yes
Input: avolittyHasher("-Infinity", {"outputLength": 8});
Output: "e1d6f97b"
Passed: Yes
Input: avolittyHasher(NaN, {"outputLength": 8});
Output: "d59ad07b"
Passed: Yes
Input: avolittyHasher("NaN", {"outputLength": 8});
Output: "59b7041f"
Passed: Yes
Input: avolittyHasher(0, {"outputLength": 8});
Output: "9346e1e0"
Passed: Yes
Input: avolittyHasher("0", {"outputLength": 8})
Output: "c3ca82be"
Passed: Yes
Input: avolittyHasher("01", {"outputLength": 8});
Output: "0d58c61d"
Passed: Yes
Input: avolittyHasher(1, {"outputLength": 8});
Output: "b8045fb1"
Passed: Yes
Input: avolittyHasher("1", {"outputLength": 8});
Output: "e888f080"
Passed: Yes
Input: avolittyHasher("10", {"outputLength": 8});
Output: "d516b3f7"
Passed: Yes
Input: avolittyHasher("", {"outputLength": 8});
Output: "2a58c3cd"
Passed: Yes
Input: avolittyHasher(void(0), {"outputLength": 8});
Output: "25cf5ca4"
Passed: Yes
Input: avolittyHasher("void(0)", {"outputLength": 8});
Output: "15a4fd45"
Passed: Yes
Input: avolittyHasher(undefined, {"outputLength": 8});
Output: "25cf5ca4"
Passed: Yes
Input: avolittyHasher("undefined", {"outputLength": 8});
Output: "eb0f94a9"
Passed: Yes
Input: avolittyHasher("🥑", {"outputLength": 8});
Output: "b0c2c9c7"
Passed: Yes
Input: avolittyHasher("🥑🥑", {"outputLength": 8});
Output: "e043f08b"
Passed: Yes
Input: avolittyHasher({"3": "🥑", "2": "🥑", "1": "🥑"}, {"ignoreObjectKeySortOrder": false, "outputLength": 8});
Output: "caff895e"
Passed: Yes
Input: avolittyHasher({"3": "🥑", "2": "🥑", "1": "🥑"}, {"ignoreObjectKeySortOrder": true, "outputLength": 8});
Output: "90656a4a"
Passed: Yes
Input: avolittyHasher({"1": "🥑", "2": "🥑", "3": "🥑"}, {"ignoreObjectKeySortOrder": true, "outputLength": 8});
Output: "90656a4a"
Passed: Yes
Input: avolittyHasher("🥑", {"outputLength": 16});
Output: "fe371194822068d1"
Passed: Yes
Input: avolittyHasher("🥑", {"outputLength": 32});
Output: "7a1241947e7587c812d5b8140eb1f260"
Passed: Yes
Input: avolittyHasher("🥑", {"outputLength": 64});
Output: "72d8b2b947633a3901b3782351f9202f132e962eae66d4c1f03a6dba11ad1f72"
Passed: Yes
Input: avolittyHasher("🥑", {"outputLength": 128});
Output: "7254953d646b74c097a12482ae7dc9143008b2b10d50b69013083652a6b7574c377022d020f9a7d270b3c23e366bf818edd5b0005d7e028e89e64dd38e08c397"
Passed: Yes