A minimal Node.js wrapper around the Cargo crate pdqhash, which is a pure Rust implementation of Meta's PDQ algorithm.
yarn add pdqhash-node
import { readFileSync } from 'fs';
import { pdqhash, pdqhashBuffer } from 'pdqhash-node';
const buf = readFileSync('image.jpg');
var hashHex = pdqhash(buf); // string | null
// or
var hashBuf = pdqhashBuffer(buf); // Buffer | null
console.log(hashHex);pdqhash(input: Buffer): string | null- Hex string PDQ hash. Returns
nullif hashing fails.
- Hex string PDQ hash. Returns
pdqhashBuffer(input: Buffer): Buffer | null- Raw PDQ hash bytes as a
Buffer. Returnsnullif hashing fails.
- Raw PDQ hash bytes as a
pdqhashWithQuality(input: Buffer): { hash: string; quality: number } | null- Hex string PDQ hash plus the quality score. Returns
nullif hashing fails.
- Hex string PDQ hash plus the quality score. Returns
hammingDistance(a: Buffer, b: Buffer): number- Computes Hamming distance between two PDQ hash Buffers.
Compute hash in hex and Buffer:
import { readFileSync } from 'fs';
import { pdqhash, pdqhashBuffer } from 'pdqhash-node';
const buf = readFileSync('image.jpg');
console.log(pdqhash(buf)); // hex string
console.log(pdqhashBuffer(buf)?.toString('hex')); // same hash from BufferGet hash with quality:
import { readFileSync } from 'fs';
import { pdqhashWithQuality } from 'pdqhash-node';
const buf = readFileSync('image.jpg');
const result = pdqhashWithQuality(buf);
if (result) {
console.log(result.hash, result.quality);
}Compute Hamming distance:
import { readFileSync } from 'fs';
import { pdqhashBuffer, hammingDistance } from 'pdqhash-node';
const a = pdqhashBuffer(readFileSync('image1.jpg'));
const b = pdqhashBuffer(readFileSync('image2.jpg'));
if (a && b) {
console.log(hammingDistance(a, b));
}