首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >哈希算法的python实现与应用

哈希算法的python实现与应用

作者头像
timerring
发布2022-07-20 14:44:25
发布2022-07-20 14:44:25
41800
代码可运行
举报
文章被收录于专栏:TechBlogTechBlog
运行总次数:0
代码可运行

Program : Regular hash algorithms

In this program, you are required to invoke the md5 and sha256 algorithms that are implemented in hashlib build-in library. Your program does the following:

  • Read the input byte array as a hex string.
  • Output the md5 digest of the input, as a hex string.
  • Output the md5 digest of the input, as a Base64 string.
  • Output the sha256 digest of the input, as a hex string.
  • Output the sha256 digest of the input, as a Base64 string…

Example Input 1

代码语言:javascript
代码运行次数:0
运行
复制
d131dd02c5e6eec4693d9a0698aff95c2fcab58712467eab4004583eb8fb7f8955ad340609f4b30283e488832571415a085125e8f7cdc99fd91dbdf280373c5bd8823e3156348f5bae6dacd436c919c6dd53e2b487da03fd02396306d248cda0e99f33420f577ee8ce54b67080a80d1ec69821bcb6a8839396f9652b6ff72a70

Example Output 1

代码语言:javascript
代码运行次数:0
运行
复制
MD5 digest of the input:
79054025255fb1a26e4bc422aef54eb4
eQVAJSVfsaJuS8QirvVOtA==

SHA256 digest of the input:
8d12236e5c4ed9f4e790db4d868fd5c399df267e18ff65c1107c328228cffc98
jRIjblxO2fTnkNtNho/Vw5nfJn4Y/2XBEHwygijP/Jg=

Example Input 2

代码语言:javascript
代码运行次数:0
运行
复制
d131dd02c5e6eec4693d9a0698aff95c2fcab50712467eab4004583eb8fb7f8955ad340609f4b30283e4888325f1415a085125e8f7cdc99fd91dbd7280373c5bd8823e3156348f5bae6dacd436c919c6dd53e23487da03fd02396306d248cda0e99f33420f577ee8ce54b67080280d1ec69821bcb6a8839396f965ab6ff72a70

Example Output 2

代码语言:javascript
代码运行次数:0
运行
复制
MD5 digest of the input:
79054025255fb1a26e4bc422aef54eb4
eQVAJSVfsaJuS8QirvVOtA==

SHA256 digest of the input:
b9fef2a8fc93b05e7701e97196fda6c4fbeea25ff8e64fdfee7015eca8fa617d
uf7yqPyTsF53Aelxlv2mxPvuol/45k/f7nAV7Kj6YX0=
solution code
代码语言:javascript
代码运行次数:0
运行
复制
import hashlib
import base64


# define the function decode_utf8
def decode_utf8(in_bytes: bytes) -> str:
    return in_bytes.decode('utf-8')


# Program: Regular hash algorithms

string_1: str = input("input the message:")
# Read the input byte array as a hex string.
message_1: bytes = bytes.fromhex(string_1)
# Output the md5 digest of the input, as a hex string.
hash1_hex: str = hashlib.md5(message_1).hexdigest()
# Output the md5 digest of the input, as a Base64 string.
hash1_bytes: bytes = hashlib.md5(message_1).digest()
hash1_base64: str = base64.b64encode(hash1_bytes).decode('utf-8')
print('MD5 digest of the input:')
print(hash1_hex)
print(hash1_base64)

# Output the sha256 digest of the input, as a hex string.
hash2_hex: str = hashlib.sha256(message_1).hexdigest()
# Output the sha256 digest of the input, as a Base64 string.
hash2_bytes: bytes = hashlib.sha256(message_1).digest()
hash2_base64: str = base64.b64encode(hash2_bytes).decode('utf-8')
print('\nSHA256 digest of the input:')
print(hash2_hex)
print(hash2_base64)
output
代码语言:javascript
代码运行次数:0
运行
复制
input the message:d131dd02c5e6eec4693d9a0698aff95c2fcab58712467eab4004583eb8fb7f8955ad340609f4b30283e488832571415a085125e8f7cdc99fd91dbdf280373c5bd8823e3156348f5bae6dacd436c919c6dd53e2b487da03fd02396306d248cda0e99f33420f577ee8ce54b67080a80d1ec69821bcb6a8839396f9652b6ff72a70
MD5 digest of the input:
79054025255fb1a26e4bc422aef54eb4
eQVAJSVfsaJuS8QirvVOtA==

SHA256 digest of the input:
8d12236e5c4ed9f4e790db4d868fd5c399df267e18ff65c1107c328228cffc98
jRIjblxO2fTnkNtNho/Vw5nfJn4Y/2XBEHwygijP/Jg=

进程已结束,退出代码为 0

input the message:d131dd02c5e6eec4693d9a0698aff95c2fcab50712467eab4004583eb8fb7f8955ad340609f4b30283e4888325f1415a085125e8f7cdc99fd91dbd7280373c5bd8823e3156348f5bae6dacd436c919c6dd53e23487da03fd02396306d248cda0e99f33420f577ee8ce54b67080280d1ec69821bcb6a8839396f965ab6ff72a70
MD5 digest of the input:
79054025255fb1a26e4bc422aef54eb4
eQVAJSVfsaJuS8QirvVOtA==

SHA256 digest of the input:
b9fef2a8fc93b05e7701e97196fda6c4fbeea25ff8e64fdfee7015eca8fa617d
uf7yqPyTsF53Aelxlv2mxPvuol/45k/f7nAV7Kj6YX0=

进程已结束,退出代码为 0

A screenshot of the console output of the program

受于文本篇幅原因,本文相关算法实现工程例如环境及相关库,无法展示出来,现已将资源上传,可自行点击下方链接下载。

python实现Hash和HMAC算法工程文件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Program : Regular hash algorithms
    • solution code
    • output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档