I wish to convert a string to md5 and to base64. Here’s what I achieved so far:
base64.urlsafe_b64encode("text..." + Var1 + "text..." +
hashlib.md5(Var2).hexdigest() + "text...")
Python raises a TypeError which says: Unicode objects must be encoded before hashing.
Edit: This is what I have now:
var1 = "hello"
var2 = "world"
b1 = var1.encode('utf-8')
b2 = var2.encode('utf-8')
result = "text" +
base64.urlsafe_b64encode("text" + b1 + "text" +
hashlib.md5(b2).hexdigest() + "text") +
"text"
Var1andVar2are strings (unicode) but themd5()andurlsafe_b64encode()functions require plain old bytes as input.You must convert
Var1andVar2to a sequence of bytes. To do this, you need to tell Python how to encode the string as a sequence of bytes. To encode them as UTF-8, you could do this:You could then pass these byte-strings to the functions: