A bloom filter is a space-efficient probablistic data structure used to check set membership. For more information, see: https://en.wikipedia.org/wiki/Bloom_filter
from bloom_filter import BloomFilter
# the constructor takes the expected number of elements and the desired error probability
number_of_elements = 1000
desired_error = 0.005
filter = BloomFilter(number_of_elements, desired_error)
# add items to the filter
filter.add("some element")
filter.add("another")
# returns True if might be in filter, False otherwise
# note: the bloom filter can return false positives, but not false negatives
filter.maybe_element("some element")
filter.maybe_element("something")
# save the bloom filter in to file for later use
out_file = open('/path/to/some/file/some_file', 'wb')
filter.write(out_file)
# read in a bloom filter that was saved to a file
in_file = open('/path/to/some/file/other_file', 'rb')
other_filter = BloomFilter.open(in_file)The bloom filter can be used to check if a given password is in a set of breached passwords (can be obtained from https://haveibeenpwned.com/).
The following are some example queries applied to the set of passwords:
?- password
True
?- correcthorsebatterystaple
True
?- qwertyuiop
True
?- sdfghjkl
True
?- qwfgfddfgfgfdff
False
?- zxokggty
False
?- bW9vY293Z29lc21vbw==
False
?- anypasswordheretotest
False
?- 123456789
True
?- 12624120720
True
?- 10195748
False
?- bloomfiltersarecool
False