
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
bktree-fast
Advanced tools
As an example, a common strategy for de-duplicating images is to compute perceptual hashes for each of the images and compare those hashes with each other. Such hashes are small compared with the images (often 32, 64, 128 bits). If the hashing function is
As an example, a common strategy for de-duplicating images is to compute perceptual hashes for each of the images and compare those hashes with each other. Such hashes are small compared with the images (often 32, 64, 128 bits). If the hashing function is good hashes that differ by a small number of bits likely relate to similar images.
While the hashing operation is typically fast, searching for similar images theoretically involves comparing the hash for a particular image with all the other hashes.
A Burkhard Keller tree is a data structure that greatly speeds up the search for hashes that differ from a target hash by no more than a specified number of bits.
This is a native C implementation of a BK tree.
In the interests of efficiency the only distance metric it supports is the Hamming distance between binary hashes, i.e. the number of bits that differ between them. That's not a restriction of BK trees in general; they work with any distance metric.
A BKTree behaves like a set: no values are stored against the hashes and adding a hash more than once has no effect.
$ npm install bktree-fast
const BKTree = require("bktree-fast");
// Make a new tree for 512 bit hashes. The hash length
// must be a multiple of 64.
const tree = new BKTree(512);
// Hashes are hexadecimal strings
const a =
"611e251612260cb60fb4afb003b142e1a36bb3db93d313c1d3cbf2c3f2d312ba" +
"c0cdc0c5c8c5c8c5c0c5c045c0c5c0c5e0c5e0cde1cde1ddc1ddc1f9c1f9c3f9";
const b =
"63be673600260db64fb4afb083b141e1a37bb3db93c1d3c193cbf2cbf2d392e3" +
"c0cdc0c5c8c5c8c5c0c5c045c0c5c0c5c0c5c0cde1cde1dde1ddc1f9c1f9c3f9";
tree.add(a, b);
// Search for all entries within 10 bits of |a|
tree.query(a, 10, (key, distance) => {
console.log(`${key} ${distance}`);
});
const tree = new BKTree(64);
The constructor takes a single argument: the number of bits in each hash. An error is thrown if this is not a multiple of 64.
tree.add(hash); // A single hash
tree.add(hash1, hash2, hash3); // Multiple hashs
tree.add([hash1, hash2], [hash3, hash4]); // Arrays of hashs
Add hashes to the tree. Handles multiple arguments and any arrays are flattened.
tree.query(hash, 10, (found, distance) => {
console.log(`${found} is ${distance} bits from ${hash}`);
});
Query the tree to find all hashes that are within the specified Hamming distance of the supplied hash. Searches slow down significantly when maxDist is large.
const found = tree.find(hash, 10);
// Returns an array of { key: "...", distance: ... }
for (const { key, distance } of found)
console.log(`${key} ${distance}`);
Find all hashes within the specified Hamming distance of the supplied hash. Returns an array of objects each of which contains a hash and its distance from the hash we're searching for. The array is ordered by ascending distance.
if (tree.has(hash)) console.log(`Got ${hash}`);
Check whether the specified hash is in the tree.
console.log(`${tree.size} unique hashes`);
Return the number of unique hashes in the tree.
tree.walk((hash, depth) => {
console.log(`${hash} is at depth ${depth} in the tree`);
});
Iterate all the hashes in the tree invoking the callback with each hash and its corresponding depth in the tree.
const dist = tree.distance(hashA, hashB);
Compute the Hamming distance between two hashes. The return value will be between 0 and the number of hash bits for this tree.
Copyright © 2020, Andy Armstrong. Released under the MIT License.
FAQs
As an example, a common strategy for de-duplicating images is to compute perceptual hashes for each of the images and compare those hashes with each other. Such hashes are small compared with the images (often 32, 64, 128 bits). If the hashing function is
The npm package bktree-fast receives a total of 60 weekly downloads. As such, bktree-fast popularity was classified as not popular.
We found that bktree-fast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.