8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png
用Python和PHP生成文件的md5 Python PHP 只有源码
admin 2021-10-16

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)

最新回复 (1)
    • 朕弟分享 | 专注小众,乐于分享!
      3
          
返回
发新帖 搜索 反馈 回顶部