You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MD5/MD5效率对比简单简洁代码.py

49 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import hashlib
import time
def mymd5(str):
md5 = hashlib.md5()
md5.update(msg)
for i in range(loop):
md5.hexdigest()
def mysha1(str):
sha = hashlib.sha1()
sha.update(msg)
for i in range(loop):
sha.hexdigest()
def mysha384(str):
sha = hashlib.sha384()
sha.update(msg)
for i in range(loop):
sha.hexdigest()
def mysha512(str):
sha = hashlib.sha512()
sha.update(msg)
for i in range(loop):
sha.hexdigest()
if __name__ == "__main__":
#str = 'a'
str = input('请输入要计算hash的文本\n')
msg = str.encode()
loop = 10000000
print("文本:",str,",计算",loop,"次hash\n")
start = time.time()
mymd5(msg)
print('md5耗时', time.time() - start, '\n')
start = time.time()
mysha1(msg)
print('sha1耗时', time.time() - start, '\n')
start = time.time()
mysha384(msg)
print('sha384耗时', time.time() - start, '\n')
start = time.time()
mysha512(msg)
print('sha512耗时', time.time() - start, '\n')