python calculate the md5 value

  md5 is a common non-reversible encryption algorithm, easy to use, fast calculation, will be used in many scenarios, such as: user-uploaded to the file name, user passwords stored in the database, download the file inspection documents are correct and so on. The following explains how to use the md5 algorithm in python.

 

A calculated value string md5

#!/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 code, output: 5d41402abc4b2a76b9719d911017c592

Calculating with a built-in string with PHP md5 function, md5 hello verification is correct.

<?php

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

Visible string md5 calculated under the python is also very easy to use hashlib library can be. This paper introduces online at python2.x can use md5 library, which can not be used in python3.x, it is not recommended to use the library.

 

md5 string calculation is relatively simple, the following look at how to calculate the value md5 file.

Two, md5 value calculation 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

Md5 file with string calculation is calculated as the direct use md5 method hashlib then hexdigests enough. Also with PHP code verification is

<?php

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

As can be seen from the results md5 is the same, the md5 value, but also so ah, my heart barely disguised pleasure. . .

If large files do, such as do a few G, the above code will certainly be out of memory, how to do it, you can read the contents of the file and block computing.

 

Third, the calculation of large files md5 value

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 

Import SYS
 Import hashlib 

DEF get_file_md5 (fname): 
    m = hashlib.md5 ()    # Create md5 objects 
    with Open (fname, ' RB ' ) AS F obj:
         the while True: 
            the Data = fobj.read (4096 )
             IF  not the Data:
                 BREAK 
            m.update (the Data)   # update the md5 objects 

    return m.hexdigest ()     # returns the md5 objects 

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

Block read the file, and then calls hashlib the update () method to update the data block md5 object, hexdigest last call () method derived md5 value.

 

Fourth, the library packaged into conventional md5.py

# ! / Usr / bin / the env Python 
# - * - Coding: UTF-. 8 - * - 

Import hashlib 

DEF get_file_md5 (file_name):
     "" " 
    computing document MD5 
    : param file_name: 
    : return: 
    " "" 
    m = hashlib. md5 ()    # create md5 objects 
    with Open (file_name, ' rb ' ) AS F obj:
         the while True: 
            the Data = fobj.read (4096 )
             IF  not the Data:
                 BREAK 
            m.update (the Data)   # update the md5 objects 

    return m.hexdigest ( )    # Returns the object md5 


DEF get_str_md5 (Content):
     "" " 
    Calculation string md5 
    : param Content: 
    : return: 
    " "" 
    m = hashlib.md5 (Content) # Create Object md5 
    return m.hexdigest ()

 

Well, calculated on md5 on here, there are different opinions of students, welcome Paizhuan together to explore, thank you.

 

Guess you like

Origin www.cnblogs.com/xiaodekaixin/p/11203857.html