[Django]-Python UnicodeEncodeError, but I have encoded the parameters to UTF-8

8👍

✅

Unicode is the problem. Hashing algorithms are designed to be used with bytes, not unicode code points. So you must choose encoding and encode your unicode strings to byte strings before applying hashing algorithm:

from hashlib import md5 

str_to_hash = unicode_str.encode('utf-8')
md5(str_to_hash).hexdigest()

There was an issue about this problem in Python tracker – investigate it for more information.

1👍

@Rostyslav has it right. Use byte strings with hashlib. May I also suggest using a source file encoding for readability? Check the message parameter. The original code had an error with \u?!! in the string. I left it out:

# coding: utf8
import hashlib

SEC_KEY = 'salt'

params = {
    u'access_token' : u'195036|6.3cf38700f.2592000.1347375600-462350295',
    u'action_link' : u'http://wohenchun.xxx.com',
    u'action_name' : u'测试一下',
    u'api_key' : u'8c0a2cded4f84bbba4328ccba22c3374',
    u'caption' : u'纯洁指数测试',
    u'description' : u'世界这么乱,装纯给谁看?到底你有多单纯,超级内涵的你,敢不敢挑战超级内涵的题目?不管你测不测,反正我是测了!',
    u'format' : u'JSON',
    u'image' : u'http://hdn.xnimg.cn/photos/hdn21/20120809/1440/h0dd1376.jpg',
    u'message' : u'尼【你难道比我更纯洁么,来测试一下吧!传送门 >>  http://wohenchun.jiongceyan.com 】\r\n\t\t\t\t\t\t\t\t\t\t',
    u'method' : u'feed.publishFeed',
    u'name' : u'人人史上最火爆测试之单纯测试',
    u'url' : u'http://wohenchun.xxx.com',
    u'v' : u'1.0'}

def renren_get_sig(params):
    data = u''.join(u'{0}={1}'.format(k,v) for k,v in sorted(params.items()))
    return hashlib.md5(data.encode('utf8') + SEC_KEY).hexdigest()

print renren_get_sig(params)

Output:

085b14d1384ba805d2d5d5e979913b27

Leave a comment