Hash tables and hashing objects.
Version history
- 1.9.0 Initial implementation
- Source:
Classes
Interfaces
Methods
(inner) bytes2hex(buf, T) → {T}
- Source:
Convert Uint8Array into a hex encoded string.
const assert = require('assert');
const { bytes2hex, hash } = require('ferrum');
// You can use this to derive a string value from hashes
assert.strictEqual(
bytes2hex(hash({ foo: "Hello World" })),
"8c363e9e1bcabde7");
// Which is useful because now the === operator works properly.
// (=== on Uint8Array compares by identity, not by content)
assert(hash(1) !== hash(1));
assert(bytes2hex(hash(1)) === bytes2hex(hash(1)));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
buf |
Uint8Array | The buffer to convert into hex |
T |
function | The target type |
Returns:
- Type
- T
(inner) defaultBuildHasher() → {function}
- Source:
This is really just a wrapper for DefaultHasher.new() that needs to be invoked twice. While it really does nothing of interest, it is provided so it can be used as a drop in replacement for other buildHasher constructors like randomBuildHasher.
A Hasher is an object that implements the Hasher interface (e.g. DefaultHasher). A buildHasher is a nullary (zero parameter function) constructor for a Hasher (e.g. DefaultHasher.new). A genBuildHasher produces a buildHasher; this function is a genBuildHasher.
const assert = require('assert');
const { hashWith, bytes2hex, defaultBuildHasher, hash } = require('ferrum');
const h1 = hash({ foo: [1, 2] });
const h2 = hashWith({ foo: [1, 2] }, defaultBuildHasher());
const h3 = defaultBuildHasher()().update({ foo: [1, 2] }).digest();
assert.strictEqual(bytes2hex(h1), bytes2hex(h2));
assert.strictEqual(bytes2hex(h1), bytes2hex(h3));
Version history
- 1.9.0 Initial implementation
Returns:
DefaultHasher.new
- Type
- function
(inner) hash(v) → {Uint8Array}
- Source:
Calculates the hash of the given value using the default hasher.
const assert = require('assert')
const { hash, DefaultHasher, bytes2hex } = require('ferrum');
assert.strictEqual(
bytes2hex(hash({ foo: [1, 2] })),
bytes2hex(DefaultHasher.new().update({ foo: [1, 2] }).digest()));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hashDirectly() → {Uint8Array}
- Source:
Convert basic data types into byte sequences.
The following serialization rules are used:
- Typed arrays into their little endian representation
- Strings as UTF-8
- As a single element Float64Array.
nullashex2bytes('6218ef48d321422fa9b4416563903f05')undefinedashex2bytes('0308394d20384acda38432c81d0654ed')trueashex2bytes('24817a77c9374571a9469a92faed50b3')falseashex2bytes('74070422bb654cf788c9bef90900a8f2'),
Other types currently result in an error, but further types may be added in the future.
const assert = require('assert');
const { hash, hashDirectly, bytes2hex, hex2bytes } = require('ferrum');
const h1 = hash(hashDirectly(0));
const h2 = hash(hex2bytes('0000000000000000'));
assert.strictEqual(bytes2hex(h1), bytes2hex(h2));
// Without hashDirectly numbers are hashed in a more complex way
// that involves the use of type tagging.
const h3 = hash(0);
assert.notStrictEqual(bytes2hex(h1), bytes2hex(h3));
Version history
- 1.9.0 Initial implementation
Parameters:
| Type | Description |
|---|---|
Returns:
- Type
- Uint8Array
(inner) hashmap(seq) → {HashMap}
- Source:
Constructs a hashmap from a sequence
const assert = require('assert');
const { hashmap } = require('ferrum');
const hm = hashmap([[{}, 42], [[], 23]]);
assert.strictEqual(hm.size, 2);
assert.strictEqual(hm.get({}), 42);
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
seq |
Sequence |
Returns:
- Type
- HashMap
(inner) hashSeq(v) → {Uint8Array}
- Source:
Hash multiple values using the default hasher.
This corresponds to repeated .update() calls.
const assert = require('assert');
const { hashSeq, bytes2hex, DefaultHasher } = require('ferrum');
assert.strictEqual(
bytes2hex(hashSeq([{}, [1,2,3]])),
bytes2hex(DefaultHasher.new().update({}).update([1,2,3]).digest()));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Sequence of anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hashSeqWith(v) → {Uint8Array}
- Source:
Hash multiple values using a custom buildHasher.
This corresponds to repeated .update() calls.
const assert = require('assert')
const { hashSeqWith, bytes2hex, randomBuildHasher } = require('ferrum');
const bh = randomBuildHasher();
assert.strictEqual(
bytes2hex(hashSeqWith([{}, [1,2,3]], bh)),
bytes2hex(bh().update({}).update([1,2,3]).digest()));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Sequence of anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hashset(seq) → {HashMap}
- Source:
Constructs a hashset from a sequence
const assert = require('assert');
const { hashset } = require('ferrum');
const hm = hashset([32, {}, 42, []]);
assert.strictEqual(hm.size, 4);
assert(hm.has([]));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
seq |
Sequence |
Returns:
- Type
- HashMap
(inner) hashUnordered(v) → {Uint8Array}
- Source:
Hash multiple values using UnorderedHasher+DefaultHasher.
This corresponds to repeated .update() call; the order
doesn't matter.
const assert = require('assert');
const {
hashSeqWith, hashUnordered, bytes2hex, unorderedBuildHasher,
defaultBuildHasher,
} = require('ferrum');
assert.strictEqual(
bytes2hex(hashUnordered([{}, [1,2,3]])),
bytes2hex(hashSeqWith([[1,2,3], {}],
unorderedBuildHasher(defaultBuildHasher()))));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Sequence of anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hashUnorderedWith(v) → {Uint8Array}
- Source:
Hash multiple values using UnorderedHasher and a custom buildHasher.
This corresponds to repeated .update() call; the order
doesn't matter.
const assert = require('assert')
const {
hashSeqWith, bytes2hex, randomBuildHasher,
unorderedBuildHasher, hashUnorderedWith,
} = require('ferrum');
const bh = randomBuildHasher();
assert.strictEqual(
bytes2hex(hashUnorderedWith([{}, [1,2,3]], bh)),
bytes2hex(hashSeqWith([[1,2,3], {}],
unorderedBuildHasher(bh))));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Sequence of anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hashWith(v) → {Uint8Array}
- Source:
Calculates the hash of the given value using some custom build hasher.
const assert = require('assert')
const { hashWith, bytes2hex, randomBuildHasher } = require('ferrum');
const bh = randomBuildHasher();
const h = hashWith(bh); // hashWith is curryable
assert.strictEqual(
bytes2hex(h({ foo: [1, 2] })),
bytes2hex(bh().update({ foo: [1, 2] }).digest()));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
v |
* | Anything that implements Hashable |
Returns:
- Type
- Uint8Array
(inner) hex2bytes(buf, T) → {T}
- Source:
Convert a hex encoded string to an Uint8Array.
const { hex2bytes, assertEquals } = require('ferrum');
assertEquals(
hex2bytes("ff00ff00"),
new Uint8Array([0xff, 0x00, 0xff, 0x00]));
Version history
- 1.9.0 Initial implementation
Parameters:
| Name | Type | Description |
|---|---|---|
buf |
Uint8Array | The buffer to convert into hex |
T |
function | The target type |
Returns:
- Type
- T
(inner) randomBuildHasher() → {function}
- Source:
Create a new buildHasher using a random seed.
This is mostly useful as a mitigation against attacks like hashdos. Using this is usually a better choice of default buildHasher than defaultBuildHasher.
A Hasher is an object that implements the Hasher interface (e.g. DefaultHasher). A buildHasher is a nullary (zero parameter function) constructor for a Hasher (e.g. DefaultHasher.new). A genBuildHasher produces a buildHasher; this function is a genBuildHasher.
const assert = require('assert')
const { hashWith, bytes2hex, randomBuildHasher, hash } = require('ferrum');
const r1 = randomBuildHasher();
const r2 = randomBuildHasher();
assert.strictEqual(
bytes2hex(hashWith({ foo: 1 }, r1)),
bytes2hex(hashWith({ foo: 1 }, r1)));
assert.notStrictEqual(
bytes2hex(hashWith({ foo: 1 }, r1)),
bytes2hex(hashWith({ foo: 1 }, r2)));
Version history
- 1.9.0 Initial implementation
Returns:
DefaultHasher.new
- Type
- function
(inner) seededBuildHasher() → {function}
- Source:
Create a new buildHasher using a seed.
A Hasher is an object that implements the Hasher interface (e.g. DefaultHasher). A buildHasher is a nullary (zero parameter function) constructor for a Hasher (e.g. DefaultHasher.new). A genBuildHasher produces a buildHasher; this function is a genBuildHasher.
const assert = require('assert')
const { hashWith, bytes2hex, seededBuildHasher, hash } = require('ferrum');
const s1 = seededBuildHasher(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]));
const s2 = seededBuildHasher(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1]));
assert.strictEqual(
bytes2hex(hashWith({ foo: 1 }, s1)),
bytes2hex(hashWith({ foo: 1 }, s1)));
assert.notStrictEqual(
bytes2hex(hashWith({ foo: 1 }, s1)),
bytes2hex(hashWith({ foo: 1 }, s2)));
Version history
- 1.9.0 Initial implementation
Returns:
DefaultHasher.new
- Type
- function
(inner) unorderedBuildHasher() → {function}
- Source:
BuildHasher for UnorderedHasher.
A Hasher is an object that implements the Hasher interface (e.g. DefaultHasher). A buildHasher is a nullary (zero parameter function) constructor for a Hasher (e.g. DefaultHasher.new). A genBuildHasher produces a buildHasher; this function is a genBuildHasher.
const assert = require('assert')
const {
hashSeqWith, bytes2hex, randomBuildHasher, unorderedBuildHasher, hash
} = require('ferrum');
const r1 = randomBuildHasher();
assert.strictEqual(
bytes2hex(hashSeqWith([23, {}], unorderedBuildHasher(r1))),
bytes2hex(hashSeqWith([{}, 23], unorderedBuildHasher(r1))));
Version history
- 1.9.0 Initial implementation
Returns:
DefaultHasher.new
- Type
- function