python哪些数据可以hash?Python基于hashlib生成文本数据的签名摘要方法总结大全
python哪些数据可以hash?Python基于hashlib生成文本数据的签名摘要方法总结大全#!usr/bin/env Python #encoding:utf-8 ''' __Author__:沂水寒城 功能:hash 模块,生成数据的签名摘要 ''' import sys from hashlib import md5 from hashlib import SHA1 from hashlib import sha224 from hashlib import SHA256 from hashlib import sha384 from hashlib import sha512 reload(sys) sys.setdefaultencoding("utf-8") def hash_func(one_text): ''' 对输入的文本数据生成摘要签名 ''' result_dict={} #MD5 hash对象 hash_md5=md5()
本文首发地址:
https://yishuihancheng.blog.csdn.net/article/details/81914364
欢迎关注我的博客【Together_CZ】,我是沂水寒城!
今天晚上遇上一个需要使用签名摘要的问题,想到了研究生期间使用hashlib模块曾经产生了需要的签名,这里就重操旧业,找出来了原来的项目文件,梳理了一下,就完成了这个需求了,这里只是为了简单地记录一下,也是希望能帮到需要的朋友,实现很简单,下面是具体的实现,里面有相应的注释,就不多解释了。
#!usr/bin/env Python #encoding:utf-8 ''' __Author__:沂水寒城 功能:hash 模块,生成数据的签名摘要 ''' import sys from hashlib import md5 from hashlib import SHA1 from hashlib import sha224 from hashlib import SHA256 from hashlib import sha384 from hashlib import sha512 reload(sys) sys.setdefaultencoding("utf-8") def hash_func(one_text): ''' 对输入的文本数据生成摘要签名 ''' result_dict={} #MD5 hash对象 hash_md5=md5() md5hash=hash_md5.copy() md5hash.update(one_text.encode('utf-8')) md5_res=md5hash.hexdigest() result_dict['MD5']=md5_res #sha1 hash对象 hash_sha1=sha1() sha1hash=hash_sha1.copy() sha1hash.update(one_text.encode('utf-8')) sha1_res=sha1hash.hexdigest() result_dict['SHA1']=sha1_res #SHA224 hash对象 hash_sha224=sha224() sha224hash=hash_sha224.copy() sha224hash.update(one_text.encode('utf-8')) sha224_res=sha224hash.hexdigest() result_dict['SHA224']=sha224_res #sha256 hash对象 hash_sha256=sha256() sha256hash=hash_sha256.copy() sha256hash.update(one_text.encode('utf-8')) sha256_res=sha256hash.hexdigest() result_dict['SHA256']=sha256_res #SHA384 hash对象 hash_sha384=sha384() sha384hash=hash_sha384.copy() sha384hash.update(one_text.encode('utf-8')) sha384_res=sha384hash.hexdigest() result_dict['SHA384']=sha384_res #SHA512 hash对象 hash_sha512=sha512() sha512hash=hash_sha512.copy() sha512hash.update(one_text.encode('utf-8')) sha512_res=sha512hash.hexdigest() result_dict['SHA512']=sha512_res return result_dict if __name__ == '__main__': hash_key_list=['MD5' 'SHA1' 'SHA224' 'SHA256' 'SHA384' 'SHA512'] #单例测试 one_text="哈尔滨工业大学" one_res_dict=hash_func(unicode(one_text)) for one_key in hash_key_list: print "{0} hash result is: {1}".format(one_key one_res_dict[one_key]) print '------------------------------------------------------------------------' text_list=["清华大学" "北京大学" 'where did you get that body from?'] for one_text in text_list: print one_text.decode('utf-8') one_result_dict=hash_func(unicode(one_text)) for one_key in hash_key_list: print "{0} hash result is: {1}".format(one_key one_result_dict[one_key])
测试结果输出如下:
MD5 hash result is: c4afb07615e340a55ff32c5aaa8e47a5 SHA1 hash result is: 527f3d3a10f6ff74b5e51c6a0f061a7d74f08bc1 SHA224 hash result is: c21e83daea421d11ae1ecf340dafb485a97da41927b4d48d53c8d4e0 SHA256 hash result is: 30d95338e25c47abb1cad388952e214258b5179a82c10149266db0da398b63b0 SHA384 hash result is: e3692274f6805feac99c9fffc09d6671b59be518892ec7beb643dcf5523c4a759dfef26312edcca6dd5c7f0d5a93b95b SHA512 hash result is: 9d6bead7b4dfdfae5784e1e70f14be478d3dc25dc5b665af0425afaf20de6a8411b5aefafdc5432763190ddbe48dc2534d4bc2e174e6bb6fd4b5b8ffdf1c62f8 ------------------------------------------------------------------------ 清华大学 MD5 hash result is: edb1d57ad7106de0708b485dc134ae3a SHA1 hash result is: e702c99732c27e927e40b96b17be038ec6fd04ce SHA224 hash result is: a413b310e5a96550dda43b04f247de63f43963186754335fcdd4dcd5 SHA256 hash result is: 07a6d4925ba8369c4fa9789968f4c55ec202c96b6f40f174f27e3b6d6b5dda90 SHA384 hash result is: 5f9a62f534d09c1866791210df6349d2d221dbedd50dc726154e17021db7c8440a9d88abd746ff01efac7cbc3882bfe9 SHA512 hash result is: 3f4d4d0fc881e4241ab76a1cefcbc642d23c3d346cd6420184d885fdb3c5032b7da00ec08c8c5fd23d3c58a991615971c0160c4d7752fed0798418ac9ce8da9f 北京大学 MD5 hash result is: 5563b44c2e138cf9ab6b0b17867944cf SHA1 hash result is: f0f0378d0e9d0160d10488fd69f2dfbb3d280841 SHA224 hash result is: 7bc770181d1dc3eb70537768e7e7f30863daeab8d2f07bd44b66dfd1 SHA256 hash result is: 21aa189366edcf73a718e3746c11854597686c1ce21f71b7eec331531857d9e5 SHA384 hash result is: 631513fcc90a6dc7b01e9b69b993db9713e38a92fefc1c9f26c68d9948bda7f8794831539ec3ead0e1aec50e17ee3b11 SHA512 hash result is: 10977a8228ac7864da5be14475f3d2c9a218bf97b9781514c8f1856d5070efa855b7fbb506ffec1a794aee27d1859892ab3d41f6a1b7cf8d91dd5fbfdf7b3c91 where did you get that body from? MD5 hash result is: 3d5bddfce92ca73cf82d69448b963a05 SHA1 hash result is: 97e2e0e50dcbf5c48d7e53b4e7c2ae97a0cf555b SHA224 hash result is: e73453e89b1100f1e44c1458b667370ad0f1e3b23babafcf7c5b0cfd SHA256 hash result is: 6dff07865a050da5722a880956672daf804dcaced206dfe097d7848091c2c4f0 SHA384 hash result is: c6eef1d520a7210517b925529c5d5bc6ec41f3637973c64912aff2d613ee2d1595419b2aa93319bd4726b91a6a3d5806 SHA512 hash result is: 69c5fee3704e128522877f07092bef1d9278685ae48b162b9f09f7a5a95263d727ab6bfdb20c2023ef8bd8c59e3b763fc09c530b5744d4928d9955bd443e72b1 [Finished in 0.2s]