我有一个要附加字符串“文本”的 ID 列表。我想检查是否有任何 id(在附加了“text”字符串之后)等于字符串“text_compare”。
奇怪的是,在散列发生之前的字符串是相等的,但是在散列发生之后,散列似乎没有产生相同的结果。下面是我的代码。您可以在 Python 命令行上对其进行测试。
import hashlib
h = hashlib.sha512()
text = 'beruk makye'
text_compare = '3beruk makye'
text_compare_hash = h.update(text_compare)
text_compare_hash = h.hexdigest()
ids = [1,2,3]
texts = []
bool_text = []
bool_text_hash = []
for id in ids:
texts.append(str(id) + text)
for t in texts:
if t == text_compare:
bool_text.append(True)
else:
bool_text.append(False)
for t in texts:
h.update(t)
t_hash = str(h.hexdigest())
if t_hash == text_compare_hash:
bool_text_hash.append(True)
else:
bool_text_hash.append(False)
print ids
# [1, 2, 3]
print texts
# ['1beruk makye', '2beruk makye', '3beruk makye']
print bool_text
# [False, False, True]
print bool_text_hash
# [False, False, False]