diff --git a/src/fnv1a.ts b/src/fnv1a.ts deleted file mode 100644 index 09465830..00000000 --- a/src/fnv1a.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Buffer } from 'buffer'; -import { Long } from './long'; - -const MASK_8 = 0xff; -const MASK_24 = 0xffffff; -const MASK_32 = 0xffffffff; - -// See http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param for the definition of these parameters; -const FNV_PRIME = new Long(16777619, 0); -const OFFSET_BASIS = new Long(2166136261, 0); -const FNV_MASK = new Long(MASK_32, 0); - -/** - * Implementation of the FNV-1a hash for a 32-bit hash value - * Algorithm can be found here: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a - * @internal - */ -export function fnv1a32(input: string, encoding?: string): number { - encoding ??= 'utf8'; - const octets = Buffer.from(input, encoding); - - let hash = OFFSET_BASIS; - for (let i = 0; i < octets.length; i += 1) { - hash = hash.xor(new Long(octets[i], 0)); - hash = hash.multiply(FNV_PRIME); - hash = hash.and(FNV_MASK); - } - return hash.getLowBitsUnsigned(); -} - -/** - * Implements FNV-1a to generate 32-bit hash, then uses xor-folding - * to convert to a 24-bit hash. See here for more info: - * http://www.isthe.com/chongo/tech/comp/fnv/#xor-fold - * @internal - */ -export function fnv1a24(input: string, encoding?: string): number { - const _32bit = fnv1a32(input, encoding); - const base = _32bit & MASK_24; - const top = (_32bit >>> 24) & MASK_8; - const final = (base ^ top) & MASK_24; - - return final; -} diff --git a/test/node/fnv1a.js b/test/node/fnv1a.js deleted file mode 100644 index 2e576ba9..00000000 --- a/test/node/fnv1a.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -const Buffer = require('buffer').Buffer; -const { fnv1a24 } = require('../register-bson'); - -describe('fnv1a', function () { - require('./specs/object-id/vectors.json').vectors.forEach(testCase => { - const hash = testCase.hash; - - let vector; - let encoding; - if (typeof testCase.vector === 'string') { - vector = testCase.vector; - encoding = 'utf8'; - } else if (typeof testCase.vectorHex === 'string') { - vector = testCase.vectorHex; - encoding = 'hex'; - } - - it(`should properly hash the string "${vector}" with a 24 bit FNV-1a`, function () { - const hashed = fnv1a24(vector, encoding); - const buff = Buffer.from([(hashed >>> 16) & 0xff, (hashed >>> 8) & 0xff, hashed & 0xff]); - expect(buff.toString('hex')).to.equal(hash); - }); - }); -}); diff --git a/test/register-bson.js b/test/register-bson.js index 15a2f2ad..7a2fcd0a 100644 --- a/test/register-bson.js +++ b/test/register-bson.js @@ -11,10 +11,7 @@ require('chai/register-expect'); const BSON = require('../lib/bson'); const { ensureBuffer } = require('../lib/ensure_buffer'); -const { fnv1a24, fnv1a32 } = require('../lib/fnv1a'); BSON.ensureBuffer = ensureBuffer; -BSON.fnv1a24 = fnv1a24; -BSON.fnv1a32 = fnv1a32; module.exports = BSON;