2017-06-03 501 views
0

嗨,我对此有点新,无法准确找出如何使其工作。将输入(密码)转换为MD5

这里是我当前的代码:

import hashlib 

def PasswordCreate(): 
    password = input(str("Please enter a password next to this text")) 
    password = hashlib.md5() 
    password.update(password.encode('utf-8')) 
    return password.hexdigest() 

PasswordCreate() 

的错误是:

AttributeError: '_hashlib.HASH' object has no attribute 'encode' 
+0

那么,什么是你的_exact_问题? – yeputons

+0

@yeputons代码不起作用,我将编辑并把问题的错误 – Josh

回答

0
import hashlib 

def PasswordCreate(): 
    user_in = input(str("Please enter a password next to this text")) 
    password = hashlib.md5() 
    password.update(user_in.encode("utf-8")) 
    return password.hexdigest() 

PasswordCreate() 

只是一个问题,在您的变量。请参阅我的代码user_in

+0

感谢代码现在运行正常,但它似乎不输出字符串(我想这样做测试)我怎样才能达到这一点? – Josh

+0

编辑:这很好我已经完成了它 – Josh

+0

只需在'print()'函数中封装'PasswordCreate()',即。 'print(PasswordCreate())' – Polymer

0

也许类似如下:

import hashlib 

def PasswordCreate(): 
    password = raw_input(str("Please enter a password next to this text: ")) 
    return hashlib.md5(password).hexdigest() 

PasswordCreate() 
+0

我使用的是Python 3.5,raw_input不是我确定的东西(至少它不起作用) – Josh

+0

另外在python 3.5你必须对'东西'进行编码,所以这不是真的有帮助 – Josh

0

就这么简单,我可以把:

import hashlib 
print(hashlib.md5(input().encode()).hexdigest()) 
0

您好乔希,

试试这个代码,

import hashlib 
def PasswordCreate(): 
    inputVar = input(str("Please enter a password next to this text: ")) 
    password = hashlib.md5() 
    password.update(inputVar.encode("utf-8")) 
    return password.hexdigest() 

# Create Variable for dipslay encoded value. 
displayPass = PasswordCreate() 
print "User Password is: ",displayPass