# coding=utf-8 """ hashlib模块 """ import unittest import hashlib import chardet class TestHashlib(unittest.TestCase): def test_md5(self): """MD5编码""" str1 = "中文str" md5 = hashlib.md5() md5.update(str1.encode('utf-8')) res = md5.hexdigest() # e3b9e843ba20257d6cd3f85668fa7c56 self.assertEqual('e3b9e843ba20257d6cd3f85668fa7c56', res) def test_sha1(self): """sha1编码""" str1 = "中文str" sha1 = hashlib.sha1() sha1.update(str1.encode('utf-8')) res = sha1.hexdigest() # 8e92214d721a0fb3a8dd749c8801da2692fe5b97 self.assertEqual('8e92214d721a0fb3a8dd749c8801da2692fe5b97', res) if __name__ == '__main__': unittest.main()