Calculate the MD5 value of the file with python

  • 2021-08-31 08:47:00
  • OfStack

md5 is a common irreversible encryption algorithm, which is easy to use and fast in calculation. It can be used in many scenarios, such as naming files uploaded by users, user passwords stored in databases, and checking whether files are correct after downloading files. The following explains how to use md5 algorithm in python.

1. Calculate the md5 value of the string


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import hashlib

reload(sys)
sys.setdefaultencoding('utf-8')

if __name__ == '__main__':
  content = "hello"
  md5hash = hashlib.md5(content)
  md5 = md5hash.hexdigest()
  print(md5)

Run the above code and output: 5d41402abc4b2a76b9719d911017c592

Use the md5 function of PHP to calculate the same string, and verify whether md5 of hello is correct.


<?php

  $content = "hello";
  $md5 = md5($content);
  var_dump($md5);  //  Output  5d41402abc4b2a76b9719d911017c592

2. Calculate the md5 value of the file


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import hashlib

reload(sys)
sys.setdefaultencoding('utf-8')

if __name__ == '__main__':
  file_name = "3383430480_51_01.jpg"
  with open(file_name, 'rb') as fp:
    data = fp.read()
  file_md5= hashlib.md5(data).hexdigest()
  print(file_md5)   # ac3ee699961c58ef80a78c2434efe0d0

File md5 calculation and string calculation is a sample, directly use md5 method of hashlib, and then hexdigests is good. Also verified by PHP code


<?php

  $file_name = "3383430480_51_01.jpg";
  $file_md5 = md5_file($file_name);
  var_dump($file_md5);  //  Output  ac3ee699961c58ef80a78c2434efe0d0

It can be seen from the results that md5 is a sample, and the value of md5 in the file is just like this. I am secretly pleased. . .

If a large file, such as a few G, the above code will definitely overflow memory. What should I do? I can read the file contents in blocks and calculate them.

3. Calculate md5 values for large files


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import hashlib

def get_file_md5(fname):
  m = hashlib.md5()  # Create md5 Object 
  with open(fname,'rb') as fobj:
    while True:
      data = fobj.read(4096)
      if not data:
        break
      m.update(data) # Update md5 Object 

  return m.hexdigest()  # Return md5 Object 

reload(sys)
sys.setdefaultencoding('utf-8')

if __name__ == '__main__':
  file_name = "mongodb_us.zip"
  file_md5 = get_file_md5(file_name)
  print(file_md5)   # 0f45cdbf14de54001e82a17c3d199a4b

4. Package into a common library md5. py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import hashlib

def get_file_md5(file_name):
  """
   Calculate the file's md5
  :param file_name:
  :return:
  """
  m = hashlib.md5()  # Create md5 Object 
  with open(file_name,'rb') as fobj:
    while True:
      data = fobj.read(4096)
      if not data:
        break
      m.update(data) # Update md5 Object 

  return m.hexdigest()  # Return md5 Object 


def get_str_md5(content):
  """
   Calculate string md5
  :param content:
  :return:
  """
  m = hashlib.md5(content) # Create md5 Object 
  return m.hexdigest()

Ok, the calculation of md5 is here, students with different opinions, welcome to pat bricks, 1 to discuss, thank you.

The above is the python calculation file MD5 value details, more about python calculation file MD5 information please pay attention to other related articles on this site!


Related articles: