Получение разных значений хеш-дайджеста для одной и той же строки при использовании хеш-функций в списке - PullRequest
0 голосов
/ 27 апреля 2019

Кажется, я получаю разные значения дайджеста для одного и того же слова в моей программе.Я не уверен, что это потому, что я храню хеш-функции в списке (так что я могу добавить в список)

Когда я использую прямые хеш-функции, дайджест хеша одинаков для одного и того же слова.Это отличается, когда я использую хэши из списка.Что я делаю не так?

Что работает

import hashlib

bloom_len = 100

def bytes_to_int(hash_value):
    return int.from_bytes(hash_value, byteorder='big')  #big-endiang format

def bloom_index(hashint):
    return hashint % bloom_len


def hashIt(word):

    m1 = hashlib.md5()
    m2 = hashlib.sha1()
    m3 = hashlib.sha256()

    m4 = hashlib.sha3_512()
    m5 = hashlib.blake2s()

    m1.update(word)
    m2.update(word)
    m3.update(word)

    m4.update(word)
    m5.update(word)


    hash_values = [m1.digest(), m2.digest(), m3.digest(), m4.digest(), m5.digest()]
    hashints = list(map(bytes_to_int, hash_values))
    indices = list(map(bloom_index, hashints))

    print(indices)


inputWord = 'sent'
word = inputWord.encode('utf-8')
hashIt(word)

inputWord = 'blue'
word = inputWord.encode('utf-8')
hashIt(word)

inputWord = 'sent'
word = inputWord.encode('utf-8')
hashIt(word)

Что НЕ работает

import hashlib


class BloomFilter():
    def __init__(self, length = 100):

        self.bloomFilterLen = length
        self.bloomFilterArray = [0] * self.bloomFilterLen

        m1 = hashlib.md5()
        m2 = hashlib.sha3_512()
        m3 = hashlib.blake2s()        

        self.hashes = [m1, m2, m3]


    def encode(self, inputWord):
        encoded_word = inputWord.encode('utf-8')
        return encoded_word

    def bytes_to_int(self, hash_value):

        return int.from_bytes(hash_value, byteorder='big')  

    def bloom_index(self, hashint):

        return hashint % self.bloomFilterLen    

    def getIndices(self, inputWord):

        word = self.encode(inputWord)

        print(word)
        hashDigests = []

        for hashFunction in self.hashes:
            hashFunction.update(word)
            print('hashFunction ', hashFunction , '\n')
            print('hashDigest ', hashFunction.digest() , '\n')

            hashDigests.append(hashFunction.digest())


        hashInts = [self.bytes_to_int(h) for h in hashDigests]    
        #print('hashInts ', hashInts)

        bloomFilterIndices = [self.bloom_index(hInt) for hInt in hashInts]
        return bloomFilterIndices

    def insert(self, inputWord):

        bloomFilterIndices = self.getIndices(inputWord)

        for index in bloomFilterIndices:
            self.bloomFilterArray[index] = 1

        print(bloomFilterIndices)


    def lookup(self, inputWord):

        bloomFilterIndices = self.getIndices(inputWord)
        print('Inside lookup')
        print(bloomFilterIndices)

        for idx in bloomFilterIndices:
            print('idx value ', idx)
            print('self.bloomFilterArray[idx] value ', self.bloomFilterArray[idx])
            if self.bloomFilterArray[idx] == 0:
                # Indicates word not present in the bloom filter
                return False

        return True            


if __name__ == '__main__':

     word = 'sent'
     bloomFilter = BloomFilter()
     bloomFilter.insert(word)


     print(bloomFilter.lookup(word))


Из первой программы - я последовательно получаю одинаковые целые индексы

  • Индексы для ' отправлено '

[61, 82, 5, 53, 87]

  • Индексы для ' синий '

[95, 25, 24, 69, 85]

  • Индексы для ' отправлено '

[61, 82, 5, 53, 87]

Для нерабочей программы целочисленные индексы отличаются, и когда я распечатываю хеш-код, они отличаются

  • Индексы для ' отправлено ' - впервые через add

[61, 53, 87]

HashDigest от MD5 для ' отправлено '

hashDigest b'x \ x91 \ x83 \ xb7\ xe9 \ x86F \ xc1 \ x1d_ \ x05D \ xc8 \ xf3 \ xc4 \ xc9 '

  • Индексы для' отправлено '- второй раз через lookup

[70, 89, 8]

HashDigest от MD5 для ' отправлено '

hashDigest b '\ x95 \ x17bC\ x17 \ x80 \ XB5 \ x9d] х \ XCA $ \ XDA \x89 \ x06 \ x16 '

Ответы [ 2 ]

0 голосов
/ 27 апреля 2019

Объекты хеш-функции не могут быть повторно использованы, вы можете переместить эти функциональные объекты в вашу функцию getIndices:

import hashlib


class BloomFilter():
    def __init__(self, length = 100):

        self.bloomFilterLen = length
        self.bloomFilterArray = [0] * self.bloomFilterLen

    def encode(self, inputWord):
        encoded_word = inputWord.encode('utf-8')
        return encoded_word

    def bytes_to_int(self, hash_value):

        return int.from_bytes(hash_value, byteorder='big')  

    def bloom_index(self, hashint):

        return hashint % self.bloomFilterLen    

    def getIndices(self, inputWord):
        m1 = hashlib.md5()
        m2 = hashlib.sha3_512()
        m3 = hashlib.blake2s()        
        hashes = [m1, m2, m3]

        word = self.encode(inputWord)

        print(word)
        hashDigests = []

        for hashFunction in hashes:
            hashFunction.update(word)
            print('hashFunction ', hashFunction , '\n')
            print('hashDigest ', hashFunction.digest() , '\n')

            hashDigests.append(hashFunction.digest())


        hashInts = [self.bytes_to_int(h) for h in hashDigests]    
        #print('hashInts ', hashInts)

        bloomFilterIndices = [self.bloom_index(hInt) for hInt in hashInts]
        return bloomFilterIndices

    def insert(self, inputWord):

        bloomFilterIndices = self.getIndices(inputWord)

        for index in bloomFilterIndices:
            self.bloomFilterArray[index] = 1

        print(bloomFilterIndices)
        bloomFilterIndices = self.getIndices(inputWord)
        print(bloomFilterIndices)


    def lookup(self, inputWord):
        print('Inside lookup')
        bloomFilterIndices = self.getIndices(inputWord)
        print(bloomFilterIndices)

        for idx in bloomFilterIndices:
            print('idx value ', idx)
            print('self.bloomFilterArray[idx] value ', self.bloomFilterArray[idx])
            if self.bloomFilterArray[idx] == 0:
                # Indicates word not present in the bloom filter
                return False

        return True            


if __name__ == '__main__':

     word = 'sent'
     bloomFilter = BloomFilter()
     bloomFilter.insert(word)

     print(bloomFilter.lookup(word))
0 голосов
/ 27 апреля 2019

Итак, я изменил код в __init __

С

m1 = hashlib.md5()
m2 = hashlib.sha3_512()
m3 = hashlib.blake2s()        

self.hashes = [m1, m2, m3]

К

self.hashes = ['md5', 'sha3_512', 'blake2s']

А затем внутри цикла for в методе getIndices ()

Изменено с

  for hashFunction in self.hashes:
        hashFunction.update(word)

К

for hashName in self.hashes:
    hashFunction = hashlib.new(hashName)
    hashFunction.update(word)

Работает сейчас!

...