PHP版本
<?php
$filename = "md5.py";
$md5file = md5_file($filename);
echo $md5file;
echo '<br>';
$str = "md5.py";
echo md5($str);
?>
Python版本
#-*-coding:utf-8-*-
import hashlib
import os
import sys
def printUsage():
print ('Usage: [python] pymd5.py s/f <string>/<filename>')
def createMD5(filePath):
if not os.path.isfile(filePath):
printUsage()
else:
tmpLength = 1024
m = hashlib.md5()
with open(filePath, 'rb') as f:
b = f.read(tmpLength)
while b != b'':
m.update(b)
b = f.read(tmpLength)
print(filePath, ' MD5:\t', m.hexdigest().upper())
def string_to_md5(string):
md5_val = hashlib.md5(string.encode('utf8')).hexdigest()
print(string, ' MD5:\t',md5_val)
return md5_val
if __name__ == '__main__':
if 3 != len(sys.argv):
printUsage()
else:
f = sys.argv[1]
d = sys.argv[2]
if f == "s":
string_to_md5(d)
else:
createMD5(d)