2013-11-27 23 views
0

我已經報廢在一起一個程序,會出現亂碼字符組的定義爲:如何散列由用戶輸入定義的字符?

hash_object = hashlib.sha256(b'Test') 

我想,使用戶輸入什麼是要散列,而不必每次都編輯程序我想要散列與「測試」不同的東西。

這是程序看起來現在的樣子,雖然第二行當前沒用,但是應該是我輸入要被散列的字符串的地方。

我該如何讓這個程序識別'x'作爲hash_object?

目前的方案:

import hashlib 
x = input("") 
hash_object = hashlib.sha256(b'Test') 
hex_dig = hash_object.hexdigest() 
print("Original hash  : ", hex_dig) 
print("Every 9 characters: ", hex_dig[::5]) 

wait = input() 

用戶保羅·埃文斯問羯羊或不是,我可以使用

hash_object = hashlib.sha256(x) 

,我不能,因爲它給了我這個錯誤:

Traceback (most recent call last): 
    File "C:\Users\einar_000\Desktop\Python\Hash.py", line 3, in <module> 
    hash_object = hashlib.sha256(x) 
TypeError: Unicode-objects must be encoded before hashing 

回答

0

庵,我是否錯過了一些東西,或者僅僅是:

hash_object = hashlib.sha256(x) 
+0

編輯約我的文章這個 – Rezic

0

所以正確答案是有點類似於保羅·埃文斯說,只需要添加

.encode() 

因此,該程序現在看起來像這樣:

import hashlib 
x = input("") 
hash_object = hashlib.sha256(x.encode()) 
hex_dig = hash_object.hexdigest() 
print("Original hash  : ", hex_dig) 
print("Every 9 characters: ", hex_dig[::5]) 

wait = input() 
相關問題