Python file MD5 SHA1 checksum calculation

Source: Internet
Author: User
Tags sha1

#coding: GBK

#md5SHA1 file Check value calculation

# How to use: python file . PY Validating Files

Import Hashlib

Import Sys

Import OS

defmd5_sum (filename): # Check Value Method

fd=open (filename, "RB") # Open File

Fd.seek (0) # move the file hit tag to Offset the location

line=fd.readline () # read the first line of the file into Line

#md5 Check value calculation

MD5=HASHLIB.MD5 ()

Md5.update (line)

#SHA1 Check value calculation

SHA1=HASHLIB.SHA1 ()

Sha1.update (line)

While line : # Looping through Files

Line=fd.readline ()

Md5.update (line)

Sha1.update (line)

fmd5=md5.hexdigest () # Generating Files MD5 Checksum value

fsha1=sha1.hexdigest () # Generating Files SHA1 Checksum value

FSUM=[FMD5,FSHA1]

Fd.close ()

Return fsum

defm_help (): # Program Help Methods

St_help= ""

****************************************

* * MD5 SHA1 Verifying use Help                

**  Input Format python md5.py file

* * Example:python md5.py D:/1/1.txt

‘‘‘

Print St_help

if __name__ = = "__main__":

Try

Filename=sys.argv[1] # Get input value

if filename== '--help ' orfilename== ': # go to the Help menu

M_help ()

elif os.path.isfile (filename): # go to File check menu

Fmd5=md5_sum (filename)

print ' File: {} '. Format (filename)

print ' MD5: {} '. Format (fmd5[0])

print ' SHA1: {} '. Format (fmd5[1])

Else: # go to the error menu

Er= ""

========== Input Error ===================== ""

Print ER

M_help ()

Except Exception:

M_help ()

This article is from the "Tuk-line" blog, please make sure to keep this source http://lovexm.blog.51cto.com/3567383/1718052

Python file MD5 SHA1 checksum calculation

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.