moolb-js

Bloom filter for javascript with pluggable hasher backend
git clone git://git.defalsify.org/moolb-js.git
Log | Files | Refs | README

commit 38a7419e3cdb59b1a9475bca9e3b03c09eca5c4a
parent 66e8ec576556b5f84237dc659eab00e1923caf13
Author: nolash <dev@holbrook.no>
Date:   Sat, 31 Oct 2020 04:13:35 +0100

Change hasher signature to single arg

Diffstat:
Mmoolb/moolb.js | 56+++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 43 insertions(+), 13 deletions(-)

diff --git a/moolb/moolb.js b/moolb/moolb.js @@ -1,9 +1,33 @@ -module.exports = { - Bloom: Bloom, - fromBytes: fromBytes, -} +let crypto = undefined; + +(function() { + if (typeof module !== 'undefined' && typeof exports !== 'undefined') { + crypto = require('crypto'); + module.exports = { + Bloom: Bloom, + fromBytes: fromBytes, + }; + } else { + function cryptoWrapper(webCrypto) { + this.crypto = webCrypto; + } + cryptoWrapper.prototype.createHash = function(b, s) { + d = new Uint8Array(b.byteLength + s.byteLength); + let i = 0; + for (; i < b.byteLength; i++) { + d[i] = b[i]; + } + for (let j = 0; j < s.byteLength; j++) { + d[i+j] = s[j]; + } + return this.crypto.subtle.digest(d); + } + crypto = new cryptoWrapper(window.crypto); + window.Bloom = Bloom; + window.bloomFromBytes = fromBytes; + } +})() -let crypto = require('crypto'); // block numbers 6000000 // false positive probability 2% @@ -34,12 +58,16 @@ function Bloom(bits, rounds, hasher) { // add entry to bloom filter // \param value to add Bloom.prototype.add = function(v) { - let a = new ArrayBuffer(4); + let a = new ArrayBuffer(v.byteLength + 4); let iw = new DataView(a); + for (let i = 0; i < v.byteLength; i++) { + iw.setUint8(i, v[i]); + } + console.log(iw, v); for (var i = 0; i < this.rounds; i++) { - iw.setInt32(0, i); - let result = this.hasher(v, iw); - console.debug(result) + iw.setInt32(v.byteLength, i); + console.log(iw); + let result = this.hasher(iw); let resultHex = Array.prototype.map.call(new Uint8Array(result), x => ('00' + x.toString(16)).slice(-2)).join(''); let resultInt = parseInt(BigInt('0x'+resultHex) % BigInt(this.bits), 10); let bytepos = parseInt(resultInt / 8, 10); @@ -53,11 +81,14 @@ Bloom.prototype.add = function(v) { // \param value to check for // \return false if not found in filter Bloom.prototype.check = function(v) { - let a = new ArrayBuffer(4); + let a = new ArrayBuffer(v.byteLength + 4); let iw = new DataView(a); + for (let i = 0; i < v.byteLength; i++) { + iw.setUint8(i, v[i]); + } for (let i = 0; i < this.rounds; i++) { - iw.setInt32(0, i); - let result = this.hasher(v, iw); + iw.setInt32(v.byteLength, i); + let result = this.hasher(iw); let resultHex = Array.prototype.map.call(new Uint8Array(result), x => ('00' + x.toString(16)).slice(-2)).join(''); let resultInt = parseInt(BigInt('0x'+resultHex) % BigInt(this.bits), 10); let bytepos = parseInt(resultInt / 8, 10); @@ -89,7 +120,6 @@ Bloom.prototype.bytes = function() { function hashBloomDefault(data, salt) { let h = crypto.createHash('sha256'); h.update(data); - h.update(salt); return Uint8Array.from(h.digest()); //return h.digest(); }