利用Python的Hashlib模块实现各种Hash算法的工具
2022/4/23 11:42:39
本文主要是介绍利用Python的Hashlib模块实现各种Hash算法的工具,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import hashlib import optparse import sys def get_params(): parser = optparse.OptionParser('Usage: <Program> -m hash_method -c content') parser.add_option('-m', '--method', dest='method', type='string', help="Specify Hash Method") parser.add_option('-c', '--content', dest='content', type='string', help='Specify the data to hash') options, args = parser.parse_args() if options.method is None or options.content is None: print(parser.usage) sys.exit(0) return options.content, options.method def hash_handler(method, content): if method == 'md5': hashobj = hashlib.md5() hashobj.update(content.encode('utf-8')) print(hashobj.hexdigest()) if method == 'sha1': hashobj = hashlib.sha1() hashobj.update(content.encode('utf-8')) print(hashobj.hexdigest()) if method == 'sha224': hashobj = hashlib.sha224() hashobj.update(content.encode('utf-8')) print(hashobj.digest()) if method == 'sha256': hashobj = hashlib.sha256() hashobj.update(content.encode('utf-8')) print(hashobj.hexdigest()) if method == 'sha512': hashobj = hashlib.sha512() hashobj.update(content.encode('utf-8')) print(hashobj.hexdigest()) if __name__ == "__main__": content_data, method = get_params() hash_handler(method, content_data)
这篇关于利用Python的Hashlib模块实现各种Hash算法的工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python