Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better examples for CustomHashing #67

Open
OR13 opened this issue Nov 22, 2023 · 1 comment
Open

Better examples for CustomHashing #67

OR13 opened this issue Nov 22, 2023 · 1 comment

Comments

@OR13
Copy link

OR13 commented Nov 22, 2023

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
Copy link
Author

OR13 commented Nov 22, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant