python gets the string MD5 value method
- 2020-10-23 21:04:17
- OfStack
Work involving MD5 values to check on the document, MD5 itself is an excellent algorithm, 1 set extent to solve the conflicts of the hash hash online also has a lot of content about the MD5, here as long as it is in one experiment, verify the validation work under 1 file, because accustomed to using the python to accomplish this task, it is also used python, learned python itself bring hashlib module, the module contains the required MD5 method, python, of course, also have special MD5 module can be used and used correctly of the same, but personal hashlib module is better in 1, today use python os, commands hashlib3 module to the experiment, among them, the first two modules mainly to can perform Linux shell in python script command, in order to verify the same below 1 file and file content if I can get the same MD5 value with hashlib module MD5 methods, Well, not to mention, the following is the procedure, a very simple verification, there are detailed comments inside the explanation is not much:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Function: Verify file MD5 value
'''
import hashlib, binascii
import md5
import os
import commands
# use python built-in hashlib library
def get_md5_value(str):
my_md5 = hashlib.md5()# To obtain 1 a MD5 Object of the encryption algorithm
my_md5.update(str) # get MD5 The message digest
my_md5_Digest = my_md5.hexdigest()# In order to 16 Base returns the message digest, 32 position
return my_md5_Digest
# use python built-in hashlib Library if m.update(a) After the m.update(b) , so that's the same thing as m.update(a+b) , to verify here 1 Under the
def get_md5_value_two(str1, str2):
my_md52 = hashlib.md5()
my_md52.update(str1)
my_md52.update(str2)
my_md52_Digest = my_md52.hexdigest()
return my_md52_Digest
# use SHA1
def get_sha1_value(str):
my_sha = hashlib.sha1()
my_sha.update(str)
my_sha_Digest = my_sha.hexdigest()
return my_sha_Digest
# using os The module system() Method to get a file MD5 value
def get_file_md5_value(filename):
return os.system('md5sum ' + filename + '|cut -f1')
# using os The module popen() Method to get a file MD5 value
def get_popen_file_md5_value(filename):
return os.popen('md5sum ' + filename + '|cut -f1')
# Gets the contents of the file MD5 value
def get_file_content_md5_value(filename):
fp = open(filename)
fp_content = fp.read()
return get_md5_value(fp_content)
# using commands The module gets the file MD5 value
def get_commands_file_md5_value(filename):
return commands.getoutput('md5sum ' + filename + '|cut -f1')
if __name__ == '__main__':
string1 = 'We are friends!!!'
string2 = 'Do agree with me?'
filename = 'Linux.txt'
result_md5_value = get_md5_value(string1)
result_sha1_value = get_sha1_value(string1)
print '-------------------------------------------------'
print ' The original string is: '+string1
print 'hashlib The module MD5 Value is: '+result_md5_value,len(result_md5_value)
print 'SHA1 Value is: '+result_sha1_value,len(result_sha1_value)
print '-------------------------------------------------'
result_md5_value = get_md5_value(string2)
result_sha1_value = get_sha1_value(string2)
print ' The original string is: '+string2
print 'hashlib The module MD5 Value is: '+result_md5_value,len(result_md5_value)
print 'SHA1 Value is: '+result_sha1_value,len(result_sha1_value)
print '---------------------- validation ---------------------------'
result_md5_value = get_md5_value(string1+string2)
result_sha1_value = get_sha1_value(string1+string2)
print ' The original string is: ', string1+string2
print 'hashlib The module MD5 Value is: '+result_md5_value,len(result_md5_value)
print 'SHA1 Value is: '+result_sha1_value,len(result_sha1_value)
print '----------------------- As a whole MD5--------------------------'
result_md52_value = get_md5_value_two(string1, string2)
print ' The original string is: ', string1+string2
print 'hashlib The module MD5 Value is: '+result_md52_value,len(result_md52_value)
print '****************************os The module gets the file MD5 A value of *******************************************'
result_file_value = get_file_md5_value(filename)
print result_file_value
result_popen_file_md5_value = get_popen_file_md5_value(filename)
print result_popen_file_md5_value
print '*****************************os The module gets the contents of the file MD5 A value of ******************************************'
result_file_content_value = get_file_content_md5_value(filename)
print result_file_content_value, len(result_file_content_value)
print '*****************************commands The module gets the file MD5 A value of ******************************************'
result_commands_file_md5_value = get_commands_file_md5_value(filename)
print result_commands_file_md5_value, len(result_commands_file_md5_value)
Here are the results:
-------------------------------------------------
The original string is: We are friends!!!
hashlib The module MD5 Value is: 469306cbddd0cc5917b9536c54e619ab 32
SHA1 Value is: 7a3f8637b61108f702052dda52e1251392228260 40
-------------------------------------------------
The original string is: Do agree with me?
hashlib The module MD5 Value is: 54f45c7d0aa071186a0ec4a030213d8a 32
SHA1 Value is: 0a2c0f3317862f7055605e1faab7f02f35f466de 40
---------------------- validation ---------------------------
The original string is: We are friends!!!Do agree with me?
hashlib The module MD5 Value is: bd77df50992d4d8b65f19d47a15132fe 32
SHA1 Value is: 83bc19fc724aaa991a0278fbb1be210bc28757fb 40
----------------------- As a whole MD5--------------------------
The original string is: We are friends!!!Do agree with me?
hashlib The module MD5 Value is: bd77df50992d4d8b65f19d47a15132fe 32
****************************os The module gets the file MD5 A value of *******************************************
80d566209acf154dcfdd25f9079792f6 Linux.txt
0
<open file 'md5sum Linux.txt|cut -f1', mode 'r' at 0x7f6d99b010c0>
*****************************os The module gets the contents of the file MD5 A value of ******************************************
80d566209acf154dcfdd25f9079792f6 32
*****************************commands The module gets the file MD5 A value of ******************************************
80d566209acf154dcfdd25f9079792f6 Linux.txt 43
If you are interested in it, you can take it to experiment 1 and paste the document Linux. txt:
1.[root@localhost/root]# Means log in to the system, where # It's the power user root The user's system prompt
2.reboot The command restarts the system
3. Turn off system use (this command can only be used by super users) :shutdown[ options ][ time ][ Warning message ]
-k : Do not really shut down just send a warning to you to all users
-r Reboot immediately after shutdown
-h: Do not start after shutdown
-f: Fast shutdown is skipped when reboot fsck
-n: Quick shutdown without passing through init The program
-c: cancel 1 One that's already running shutdown
4. File operation command
ls -a List all the file names under the directory including the hidden files, i.e. . "The beginning of the document
ls -l List files in long format
ls -F Append the name of the file listed 1 A symbol (such as an executable file plus * Directory to add / )
mkdir filename Create a new directory
rmdir filename Delete directories that have been created
cd filename Go to file directory
cp -r The source file The destination file Copy the file
rm -r filename Delete this directory and all subdirectories under it
rm -f Force a file to be deleted without confirmation
rm -i filename Delete the file
mv Source file path Destination file path Move file command
mv filename1 filename2 rename
cat filename Displays the text file contents
cat file1 file2 file3 In turn, show 3 A file
cat -n filename Displays the line number as well as the document content
more filename Display text files (more content)
pwd Display current path
5. Users and user group management
useradd username Create a new user
userdel username Delete user
groupadd groupname Create a new user group
groupdel groupname Delete user groups
su -username Grant privileges to ordinary users or other identities to superusers
chomd[who][+|-|=][mode]filename Dare to change access to files or directories
chown[ User: group ] file Use to change the owner and group of a file or directory
6. Process management command
ps -e Displays all processes
ps -f The format
ps -l Long format
top Dynamic display of current process and other input, can add parameters:
q You can exit the page you are viewing
(space) refresh the display immediately
h/? Displays the main screen
m Toggles display memory information
t Toggles display process and CPU information
c Toggles display command name and full command line
M Sort according to the size of the resident memory
P According to the CPU Sort by percentage size
kill Commands are used to kill processes in the background in two main ways:
kill[-s signal ] Process of no. Kill the specified process
kill -l Display process list
cron The command is used to complete a timed task and is not started manually
cron -l View what already exists cron task
7. Disk and file commands
df -k Displays disk space usage for each partition
mount[ options ] equipment directory Mount the file
umount[-f]directory Mandatory uninstall 1 Three file systems
8. Software installation command
tar -c filename create 1 A new file
tar -t filename View the contents of the archive file
tar -x filename Dissolves the contents of an archive file
tar -f filename Specifies the name of the file
tar -v filename Display process information
tar -z filename Adopt compression mode
rpm -i packagename The installation package
rpm -U packagename Upgrade package
rpm -qa Lists all packages that have been installed
rpm -qf filename Query which package a particular file belongs to
rpm -qi packagename Query the functionality of a package
rpm -e packagename Delete the package
9.vim use
vim There are 3 Mode: command line mode, text input mode, terminal mode
The input vim To enter vim Editor, click ESC To enter command line mode, click i Enter text editing mode input: you can enter the final line mode
Enter in the last line mode q You can quit; The input wq You can save the operation and exit; The input q ! Forced out of
10.head/tail
head -n x filename View the front of the file x line
tail -n y filename After viewing the file y line
head filename View the front of the file 10 line
tail filename After viewing the file 10 line
tail -f filename After the show 10 Line content, and automatically displays the new file content after the content has been added
11. The network configuration
ifconfig -a View all network card status information
ifconfig -V To view ifconfig Command version information
ifconfig -s View statistics
ifconfig eth ipaddress Set the nic IP
ifconfig eth down Close the network card
ifconfig eth up Activate the network card
ifconfig eth netmask netmaskaddress Sets the subnet mask address of the network card
ifconfig eth ipaddress netmask netmaskaddress At the same time set up IP And the subnet mask address
ifconfig eth Check the status of the network card
route View routing information
route add -net ipaddress netmask netmaskaddress dev eth Specify the interface
netstat Display network situation
ping Detect network connectivity
nslookup ip Look at the IP Corresponding domain name
host domainname Query for the corresponding domain name IP
12. time
/sbin/hwclock --show View the current hardware time
/sbin/hwclock --hctosys Synchronize hardware time to Linux system
/sbin/hwclock --systohc will Linux Time synchronization to hardware time
13. The document
df -k In order to KB Displays the information of each partition in units
df -a Displays all partitions including size as 0 The partition
df -T Display partition type
du -b /file Displayed in bytes file The size of each folder under the folder
du -ks file In order to KB Is unit display file Total folder size
iostat According to CPU In the case
tar -c Create a package
tar -x Unpack the
tar -t Lists the contents of the package
tar -r Adds a file to the specified package
tar -u Update the files in the package
tar -zcvf file.tar.gz file The compression file
tar -zxvf file.tar.gz file unzip file.tar.gz
find -name file1 To find the file1
grep[ Command options ] The matching pattern to look for [ The file to look for ] Find files
grep str1 file in file Find string in str1
grep -r str1 /file in /file Find the string in all files under its subdirectory str1
ps -ef | grep filename Look for contains in all processes filename Process information of
14 service
service tomcat4 start Start the tomacat4 service
service tomcat4 stop Shut down tomacat4 service
That's all for today's experiment.