我有一个我想用字符串'text'附加的id列表。 我想检查是否有任何ids(在附加'text'字符串后)等于字符串'text_compare'。SHA512 Python为同一个字符串产生了不同的结果
奇怪的是,散列之前的字符串是相同的,但散列发生后,散列似乎没有产生相同的结果。以下是我的代码。您可以在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]
感谢,我在使用MD5时遇到同样的问题,我犯了同样的错误 – clarenswd