bloom-filters icon indicating copy to clipboard operation
bloom-filters copied to clipboard

Better examples for CustomHashing

Open OR13 opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe.

It was confusing to figure out how to use the CustomHashing feature, and I discovered that naively constructing numbers from hex encoded cryptographic hashes, such as sha256 or shake256 can lead to scenarios where the filter always returns true.

Describe the solution you'd like

An example showing how to use cryptographic hashes, more precisely, how to safely convert from a hex encoded string to a number.... is it ok to truncate? Why not use BigInt?

Some explanation of how to use standard hash functions safely would be awesome.

Acceptation criterias

unit tests for sha256 and shake256.

Additional context

in some environments (regulated / security oriented) only specific hash functions are allowed, it would be nice to have examples that are compliant to build from.

OR13 avatar Nov 22 '23 16:11 OR13

For example, is this kind of thing ok:

class CustomHashing extends Hashing {
  serialize(element: Buffer, seed: number) {
    if (!seed) {
      seed = defaultSeed
    }
    const digester = new jsSHA("SHA-256", "ARRAYBUFFER");
    const message = Buffer.concat([Buffer.from(seed.toString(16), 'hex'), element])
    digester.update(message)
    const hash = digester.getHash("HEX");
    return Number(BigInt.asUintN(42, BigInt(`0x${hash}`)))
  }
}
const filter = new BloomFilter(size, strength)
filter.seed = filterSeed
filter._hashing = new CustomHashing()

OR13 avatar Nov 22 '23 16:11 OR13